答案 0 :(得分:0)
首先,我们使用Year
将您的Week
+ pandas.to_datetime
转换为日期时间
from datetime import datetime as dt
df['Date'] = pd.to_datetime(df['Year'].astype(str) + '-' + df['Week'].astype(str) + '-1', format='%Y-%W-%w')
然后,我们根据here的注释中的定义定义函数:
from math import ceil
def week_of_month(dt):
""" Returns the week of the month for the specified date.
"""
first_day = dt.replace(day=1)
dom = dt.day
adjusted_dom = dom + first_day.weekday()
return int(ceil(adjusted_dom/7.0))
最后,我们将该函数应用于Date
列以获得月份的Weeknumber
:
df['Week'] = df['Date'].apply(week_of_month)
哪种产量:
print(df)
Year Month Week numOfTrips Date
0 2011 July 5 2608 2011-07-25
1 2011 August 1 6852 2011-08-01
2 2011 August 2 8092 2011-08-08
3 2011 August 3 7674 2011-08-15
4 2011 August 4 7065 2011-08-22
5 2011 August 5 3896 2011-08-29
6 2011 September 5 4182 2011-08-29
注意,由于您没有准确的日期,因此最后一行的准确性不是100%。如果我们有确切的日期,这将是100%准确。
答案 1 :(得分:0)
我找到了一个相对长且丑陋的解决方案,似乎可行。如果有错误/错误或更干净的实现,请告诉我。
我的方法:使用示例2011 July 30 2608
m = list(calendar.month_name).index('July') # turn month name into int
cal = calendar.Calendar()
weeks = cal.monthdatescalendar(2011,7) # get weeks for that month in the year
2011
yr = cal.yeardatescalendar(2011, width=12)
此代码将获取年份并将其存储在4D列表中:月垃圾(在我们的示例中为12个月的1次垃圾),月,周,日。要进行查询,请在7月的第5周的所有天进行类似yr[0][6][4][:]
的操作。因此,下一步是将其转换为可容纳所有星期的2D阵列。
flat = [week for month in yr[0] for week in month]
dates = np.array(flat)
由于dates
返回列表的方式,该cal.yeardatescalendar()
将保留重复项。我们删除了这些:
new_dates = []
for date in range(len(dates)):
if not(np.array_equal(dates[date], np.array(dates[date-1]))):
new_dates.append(dates[date])
此时,您具有2D星期数组,与您的周数保持一致。就像您去here并检查30
的第2011
周一样,您会看到它是7月的第5周。
30
周 the_week = new_dates[:][30]
for week in range(len(weeks)):
if np.array_equal(the_week, np.array(weeks[week])):
save_week_num = week+1
print(save_week_num) # for our example it will print 5
它在函数中:
def week_of_month(year, month, y_week):
m = list(calendar.month_name).index(month)
cal = calendar.Calendar()
weeks = cal.monthdatescalendar(year,m)
yr = cal.yeardatescalendar(year, width=12)
flat = [week for month in yr[0] for week in month]
dates = np.array(flat)
new_dates = []
for date in range(len(dates)):
if not(np.array_equal(dates[date], np.array(dates[date-1]))):
new_dates.append(dates[date])
the_week = new_dates[:][y_week] # for 2013, replace this with: the_week = new_dates[:][y_week-1]
number = 0
for week in range(len(weeks)):
if np.array_equal(the_week, np.array(weeks[week])):
save_week_num = week+1
number = save_week_num
return number
要在问题运行的DataFrame
上对其进行测试,请执行以下操作:
df['Week'] = df.apply(lambda row: week_of_month(row.Year, row.Month, row.Week), axis=1)