I have a pandas dataframe with Datetime as index. The index is generally monotonically increasing however there seem to be a few rows don't follow this tread. Any quick way to identify these unusual rows?
Consider the following demo:
In [156]: df
Out[156]:
val
2017-01-01 0.889887
2017-01-02 0.838433
2017-01-03 0.977659
2017-01-04 0.750143
2017-01-05 0.271435
1970-01-01 0.138332 # <---- !!!
2017-01-07 0.673203
2017-01-08 0.497589
1999-01-01 0.592959 # <---- !!!
2017-01-10 0.818760
In [157]: df.loc[df.index.to_series().diff() < pd.to_timedelta('0 seconds')]
Out[157]:
val
1970-01-01 0.138332
1999-01-01 0.592959
In [158]: df.index.to_series().diff() < pd.to_timedelta('0 seconds')
Out[158]:
2017-01-01 False
2017-01-02 False
2017-01-03 False
2017-01-04 False
2017-01-05 False
1970-01-01 True
2017-01-07 False
2017-01-08 False
1999-01-01 True
2017-01-10 False
dtype: bool
In [159]: df.index.to_series().diff()
Out[159]:
2017-01-01 NaT
2017-01-02 1 days
2017-01-03 1 days
2017-01-04 1 days
2017-01-05 1 days
1970-01-01 -17171 days
2017-01-07 17173 days
2017-01-08 1 days
1999-01-01 -6582 days
2017-01-10 6584 days
dtype: timedelta64[ns]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With