UpperEastSide
UpperEastSide

Reputation: 137

Python: How to create an array of datetime, and extract the corresponding year, month, day, hour for each index in array before binning

I can create a list of datetimes between 1994 and 2020 as follows:

from datetime import datetime, timedelta
# Create datetime for plotting
start_date = datetime(1994,1,1)
start_date_yr = start_date.year
time = [start_date + timedelta(days=x) for x in range(9553)]

'time' is a list, and is useful for plotting my 'y' data as a function of time. My 'y' data is a pandas series with dimension (9553,) containing some NaNs.

However, I want to plot my 'y' data as a function of day of the year, or month, or year. In MATLAB, I would use the function 'datevec' to get these corresponding years, months, days with same dimension (9553,) from variable 'time'.

I want to bin my 'y' data to get the annual cycle (either for each day of the year or each month), and the yearly averages (using all data corresponding to a given year).

How can I obtain a time array (datetime, year, month, day) with dimension (9553,), and how can I bin my 'y' data?

Upvotes: 0

Views: 1780

Answers (1)

rak3sh
rak3sh

Reputation: 134

Make a list of tuples:

[(datetime1, year1, month1, day1), (datetime2, year2, month2, day2), (datetime3, year3, month3, day3) ]


mydates = []
for date in time:
    mydates.append(tuple((t, t.strftime('%Y'), t.strftime('%m'), t.strftime('%d'))))

Upvotes: 1

Related Questions