Reputation: 561
I understand how to make a date_range in pandas using the freq option. However, I do not know how to use it to do two frequencies at once (or do I need a loop for this)?
I am trying to make an hourly date range for only july for a span over some years.
I have tried:
In: pd.date_range('1951-07-01','1955-07-01',freq='AS')
Out: DatetimeIndex(['1952-01-01', '1953-01-01', '1954-01-01', '1955-01-01'], dtype='datetime64[ns]', freq='AS-JAN')
And also the hourly frequency.. But what I want is a date range that spans over multiple years, but only an hourly frequency for the month of july.
I do not want any months other than july in my date_range.
Any hints are appreciated, and if a loop is necessary?
Upvotes: 1
Views: 1258
Reputation: 862701
You can create hours frequency with start and end year
and then filter only july
s:
d = pd.date_range('1951-07-01','1955-07-01',freq='H')
d = d[d.month == 7]
print (d)
DatetimeIndex(['1951-07-01 00:00:00', '1951-07-01 01:00:00',
'1951-07-01 02:00:00', '1951-07-01 03:00:00',
'1951-07-01 04:00:00', '1951-07-01 05:00:00',
'1951-07-01 06:00:00', '1951-07-01 07:00:00',
'1951-07-01 08:00:00', '1951-07-01 09:00:00',
...
'1954-07-31 15:00:00', '1954-07-31 16:00:00',
'1954-07-31 17:00:00', '1954-07-31 18:00:00',
'1954-07-31 19:00:00', '1954-07-31 20:00:00',
'1954-07-31 21:00:00', '1954-07-31 22:00:00',
'1954-07-31 23:00:00', '1955-07-01 00:00:00'],
dtype='datetime64[ns]', length=2977, freq=None)
Upvotes: 2