I want a column with day of year. How do I calculate day of the year with 15-minute interval data which are resampled to daily entries. The following code produces day of year for each 15-minute interval but I only want day of year for each day.
I also tried to resample "Time" but that does not work. Anyone have any suggestions as to how I might get day of year?
df = pd.read_csv("D:/time-series.csv")
df = df.set_index(pd.DatetimeIndex(df["Time"]))
day_of_year = pd.DatetimeIndex(df["Time"]).dayofyear
#time = df["Time"].resample("24H")
pp = df["precip"].resample("24H").sum()
Current result
day_of_year
Int64Index([182, 182, 182, 182, 182, 182, 182, 182, 182, 182,
...
366, 366, 366, 366, 366, 366, 366, 366, 366, 366],
dtype='int64', name='Time', length=30720)
Expected result
day_of_year
Time
2017-07-01 182
2017-07-02 183
2017-07-03 184
2017-07-04 185
2017-07-05 186
...
2020-12-27 362
2020-12-28 363
2020-12-29 364
2020-12-30 365
2020-12-31 366
CodePudding user response:
Typing this in the blind as I don't have your data file:
df = pd.read_csv("D:/time-series.csv", parse_dates=["Time"], index_col="Time")
pp = df["precip"].resample("24H").sum()
day_of_year = pp.index.dayofyear
If you want a dataframe:
day_of_year = pd.DataFrame({
"day_of_year": pp.index.dayofyear
}, index=pp.index)