I'm dealing with problem how to create date range in reverse. I'm trying this:
import pandas as pd
dates = pd.date_range(end='2/08/2014', periods=104, freq='W-Sat', closed = None)
Always i get from later to sooner like following:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-02-18, ..., 2014-02-08]
Length: 104, Freq: W-SAT, Timezone: None
I want to range starting with '2014-02-08'.
Thanks
Solution:
Thanks for pointing out built-in reverse functionality, you need to go back and create DatetimeIndex to use it in Time Series like this:
dTmp = pd.date_range(end='2/08/2014', periods=104, freq='W-Sat', closed = None)
dates = [d for d in reversed(dTmp)]`
Use could get this directly with fancy indexing:
pandas.date_range(end='2/08/2014', periods=104, freq='W-Sat')[::-1]
You can do this natively by specifying a negative frequency:
import pandas as pd
freq = '-1W-Sat'
dates = pd.date_range(start='2/08/2014', periods=104, freq=freq, closed = None)
note that the start
parameter was used.
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