简体   繁体   English

根据日期范围更新 Pandas DataFrame 时间列

[英]update Pandas DataFrame time column based on a date range

I have uploaded a big file and created a DataFrame for it.我上传了一个大文件并为其创建了一个 DataFrame。

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.原因是我想调整夏令时,而我正在使用的列表是 GMT 时间,所以我需要调整它的时间戳。

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.我想写一个 function,首先根据是夏季还是冬季将 6 或 7 小时添加到 DataFrame。

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']如果有可能,如果时间戳 > 16:00 + 1 天,我想更新日期列,日期列称为 df['Creation_Date']

This should work for the function if it is wintertime.如果是冬天,这应该适用于 function。

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:现在我猜你还想遍历你的 df 并分别更新时间,你可以使用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM