1

I am very new to Python and cannot seem to solve the problem on my own. Currently I have a dataset which I already converted to a DataFrame using pandas which has a datetimeindex according to yyyy-mm-dd-HH-MM-SS with time stamps of minutes. The attached figure shows the already interpolated dataframe.

enter image description here

Now I want to convert the date/datetimeindex to week numbers to plot the corresponding HVAC Actual, Chiller power etc. to their week number. The index already was set to time but I got an error telling that 'Time' was not recognized in the columns. I tried to recall the index like in the code below and from there create a new column using dt.week

building_interpolated = building_interpolated.set_index('Time')
building_interpolated['Week number'] =
building_interpolated['Time'].dt.week

If I am correct this should create a new column called Week number with the week number in it. However, I still get an error telling that ['Time'] is not in the columns (see figure below)

enter image description here

Anyone who can help me?

Regards, nooby Boaz ;)

b-roijers
  • 11
  • 2

1 Answers1

0
df.index = df.index.to_series().dt.isocalendar().week
BloomShell
  • 833
  • 1
  • 5
  • 20
  • By this code you convert the datetimeindex to the weeknumber but then in series form if I am understanding it correctly right? – b-roijers Sep 14 '22 at 15:34
  • `.to_series()` is necessary to apply `.dt`. If you want to apply something more linear, you can simply apply `df.index = pd.to_datetime(df.Time).dt.isocalendar().week ` and then rename the index with `df.index.name = "Time"` – BloomShell Sep 14 '22 at 15:39
  • If you think this solves your problem, accept the answer – BloomShell Sep 14 '22 at 17:37