1

I tried multiple ways but they always give me an error. The most common error I get is :

AttributeError: 'Timestamp' object has no attribute 'astype'

Here is the line where I try to convert my element :

df.index.map(lambda x: x - oneSec if (pandas.to_datetime(x).astype(int) / 10**9) % 1 else x)

I tried x.astype(int) or x.to_datetime().astype(int)

TheHaricover
  • 47
  • 1
  • 6
  • Can you add some data sample? – jezrael Mar 17 '21 at 11:30
  • I don't use any data here, only a datetime index. If you're talking about the datetime index here's an example : `2019-01-10 15:00:00` – TheHaricover Mar 17 '21 at 11:55
  • what problem are you trying to solve? basically you should be able to use `df.index.values.astype(np.int64)` which gives you nanoseconds since the epoch. you can work your way on from there. – FObersteiner Mar 17 '21 at 12:54
  • I'm trying to remove one second in the indexes that have one second too much. So not all the indexes – TheHaricover Mar 17 '21 at 13:59
  • what do you mean by "one second too much"? could you please add a [mre] with exemplary input and desired output? that would make it much easier for people to come up with a good answer (and you'll get it quicker). – FObersteiner Mar 17 '21 at 14:21

1 Answers1

0

I think here is necessary use Index.where:

df = pd.DataFrame(index=(['2019-01-10 15:00:00','2019-01-10 15:00:01']))

df.index = pd.to_datetime(df.index)

mask = df.index.second == 1
print (mask)
[False  True]

df.index = df.index.where(~mask, df.index - pd.Timedelta(1, unit='s'))
    
print (df)
Empty DataFrame
Columns: []
Index: [2019-01-10 15:00:00, 2019-01-10 15:00:00]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I got `TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''` for this line `df.index = df.index.where(~mask, df.index - oneSec)` I remove the `% 1` and it gave me the same error – TheHaricover Mar 17 '21 at 11:54