Given the following exemplary Pandas DataFrame
x
:
a b
2014-08-07 0.1 2.0
2014-08-18 0.2 4.0
2014-12-16 0.3 0.0
2015-01-16 0.4 2.3
2015-02-16 0.5 2.1
2015-03-18 0.6 7.0
The indices are of the type datetime.date
.
I want to write a function that takes a parameter start
of the type datetime.datetime
such that it gives me the biggest index which is smaller then start
.
For example, for start = datetime.datetime(2015, 1, 20, 17, 30)
the biggest index that is smaller then start
is, is 2015-01-16
.
This would give me the most recent change in a
and b
namely x.loc[dt(2015,1,16)]
.
The pandas asof function is meant for this:
x.index.asof(start)
It can be used on series or on datetime indexes.
See:
http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DatetimeIndex.asof.html
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