简体   繁体   English

使用 Pandas 将周数转换为周(日期)

[英]Convert week number to week (date) with Pandas

How can I convert dt.week (which returns the week number) to show the week date?如何转换 dt.week(返回周数)以显示周日期? My current code:我当前的代码:

my.net_diet_raw['Date & Time'].dt.week

Returns:退货:

1      22
2      22
3      22
4      22
       ..
176    30
177    30
178    30
179    30
180    30
Name: Date & Time, Length: 181, dtype: int64

I would like 22 to appear as 05-25-2020, 23 to appear as 06-01-2020, etc.我希望 22 显示为 05-25-2020,23 显示为 06-01-2020,等等。

Data:数据:

0    19732166  2020-05-31  Breakfast   
1     4016406  2020-05-31  Breakfast   
2        1132  2020-05-31  Breakfast   
3    19732166  2020-05-31      Lunch   
4        1009  2020-05-31      Lunch   
..        ...         ...        ...   
176      5749  2020-07-23      Lunch   
177     20037  2020-07-23      Lunch   
178   4159294  2020-07-23      Lunch   
179  20402843  2020-07-23      Snack   
180  23053329  2020-07-23      Snack

Check查看

pd.to_datetime(my_net_diet_raw['Date & Time']).dt.to_period('w').astype(str).str.split('/').str[0]

Assuming that y.net_diet_raw['Date & Time'].dt is a DatetimeProperties object , you should be able to use y.net_diet_raw['Date & Time'].dt.date to access the date (you don't need y.net_diet_raw['Date & Time'].dt.week ).假设y.net_diet_raw['Date & Time'].dtDatetimeProperties object ,您应该能够使用y.net_diet_raw['Date & Time'].dt.date来访问日期(您不需要y.net_diet_raw['Date & Time'].dt.week )。 Read more at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.html and https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.date.html .https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.htmlhttps://pandas.pydata.org/pandas-docs/stable/reference/api阅读更多内容/pandas.Series.dt.date.html

If the above doesn't fit your needs, then, you might want to try this:如果以上不符合您的需求,那么,您可能想试试这个:

Converting DatetimeProperties.dt.week to DatetimeProperties.dt.date : If you wanted to get the first days of a certain number of weeks, you could do something like this (assuming that y.net_diet_raw['Date & Time'].dt.week is a Series of integers (like in your question)):DatetimeProperties.dt.week转换为DatetimeProperties.dt.date如果您想获得特定周数的第一天,您可以这样做(假设y.net_diet_raw['Date & Time'].dt.weekSeries整数(如您的问题):

datetimes_of_first_day_of_each_week = my_net_diet_raw['Date & Time'].dt.week.apply(lambda week: datetime.strptime(f'2020 {week - 1} 1', '%Y %W %w'))

Series.apply() allows you to specify a function to compute and return a value for each given value in the original series to construct a new Series . Series.apply()允许您指定一个 function 来为原始系列中的每个给定值计算并返回一个值,以构建一个新Series Here, it is creating a new datetime object (for each week number) where the date is of the first day of the week (Monday, in this case), and finally returning each to create a new Series .在这里,它正在创建一个新的datetime时间 object(对于每个星期的编号),其中日期是一周的第一天(在本例中为星期一),最后返回每个以创建一个新的Series

This worked for me.这对我有用。 Is this what you are looking for?这是你想要的?

    df['DATE']+pd.to_timedelta(6-df['DATE'].dt.weekday, unit="d")

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

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