Im currently working on a project in Python (Pandas module). What I want to do is split a date_range into equally sized intervals.
import pandas as pd
startdate='2014-08-08'
enddate='2014-08-11'
n=3
pd.date_range(start=startdate,end=enddate)
What I would like is some way for it to return the intermediate dates as string, for example:
startdate='2014-08-08'
intermediate_1='2014-08-09'
intermediate_2='2014-08-10'
enddate='2014-08-11'
This is an example with days, but I would like to be able to do the same for hours or minutes. Is there a way to do this in the current Pandas module? Any help would be greatly appreciated.
Regards, Alex
You can use np.split
to split your array, this returns a an array of datetimeindex values so we have to access the value element using [0]
we can then call the attribute date()
and cast to str:
In [238]:
startdate='2014-08-08'
enddate='2014-08-11'
n=3
d = pd.date_range(start=startdate,end=enddate)
d
Out[238]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-08-08, ..., 2014-08-11]
Length: 4, Freq: D, Timezone: None
In [249]:
# split to produce n-1 blocks
splits = np.split(d, n-1)
# show the contents
for t in splits:
print(t)
# assign each element, then access the value using [0] and access the date attribute and cast to str
intermediate_1 = str(splits[0][0].date())
intermediate_2 = str(splits[-1][0].date())
# show intermediate_1 which shows it is a str representation of the date
intermediate_1
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-08-08, 2014-08-09]
Length: 2, Freq: D, Timezone: None
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-08-10, 2014-08-11]
Length: 2, Freq: D, Timezone: None
Out[249]:
'2014-08-08'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.