-2

I have uploaded a big file and created a DataFrame for it.

Now i want to update some of the columns containing timestamps as well if possible update columns with dates based on that.

The reason is that i want to adjust for daylight saving time, and the list i am working with is GMT time so i need to adjust the timestamps on it.

Example that works:

df_winter2['Confirmation_Time'] = pd.to_datetime(df_winter2['Confirmation_Time'].astype(str)) + pd.DateOffset(hours=7)

df_summer['Confirmation_Time'] = pd.to_datetime(df_summer['Confirmation_Time'].astype(str)) + pd.DateOffset(hours=6)

I want to write a function that first add the 6 or 7 hours to the DataFrame based on if it is summertime or wintertime.

If it is possible as well i want to update the date column if the timestamp is > 16:00 with + 1 day, the date column is called df['Creation_Date']

  • Please add a self-contained example that reproduces your problem (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)). – Timus Dec 09 '22 at 14:01

1 Answers1

0

This should work for the function if it is wintertime.

def wintertime(date_time):


year, month, day = dt.timetuple()[0:3]

if (month < 3) or (month == 12 and day < 21):
    return True

else:
    return False

Now I am guessing you also want to loop through your df and update the time respectively which you could do with the following:

for i, length in enumerate (df):
   date_time = df['Confirmation_Time'][i]

if wintertime(date_time):

   df['Confirmation_Time'][i] = pd.to_datetime(df['Confirmation_Time'][i].astype(str)) + pd.DateOffset(hours=7)

else:

   df['Confirmation_Time'][i] = pd.to_datetime(df['Confirmation_Time'][i].astype(str)) + pd.DateOffset(hours=6)

return df
Niko
  • 144
  • 8
  • Thank you, not sure why but i cannot get it to work, after the function i check if wintertime is True but it is False even though it should be True. – Peter Ramstedt Dec 10 '22 at 13:32
  • If you have not found a solution: try dt = df['Confirmation_Time'][0]; testtuple = dt.timetuple()[0:3]; print(testtuple) to see if you get an output – Niko Dec 12 '22 at 07:57