熊猫:如何从周和年创建日期时间对象?

时间:2017-08-01 11:43:20

标签: python pandas datetime numpy

我有一个数据框,提供两个整数列,其中包含年份的一年和一周:

import pandas as pd
import numpy as np
L1 = [43,44,51,2,5,12]
L2 = [2016,2016,2016,2017,2017,2017]
df = pd.DataFrame({"Week":L1,"Year":L2})

df
Out[72]: 
   Week  Year
0    43  2016
1    44  2016
2    51  2016
3     2  2017
4     5  2017
5    12  2017

我需要从这两个数字创建一个datetime-object。

我尝试了这个,但它引发了一个错误:

df["DT"] = df.apply(lambda x: np.datetime64(x.Year,'Y') + np.timedelta64(x.Week,'W'),axis=1)

然后我尝试了这个,它有效,但是给出了错误的结果,就是它完全忽略了一周:

df["S"] = df.Week.astype(str)+'-'+df.Year.astype(str)
df["DT"] = df["S"].apply(lambda x: pd.to_datetime(x,format='%W-%Y'))

df
Out[74]: 
   Week  Year        S         DT
0    43  2016  43-2016 2016-01-01
1    44  2016  44-2016 2016-01-01
2    51  2016  51-2016 2016-01-01
3     2  2017   2-2017 2017-01-01
4     5  2017   5-2017 2017-01-01
5    12  2017  12-2017 2017-01-01

我真的迷失在Python的datetime,Numpy的datetime64和pandas Timestamp之间,你能告诉我它是怎么回事吗?&#39 ; s做得对吗?

我使用Python 3,如果它与任何方式相关。

4 个答案:

答案 0 :(得分:9)

您需要%w来指定哪一天是一周的第一天:

df["DT"] = pd.to_datetime(df.Week.astype(str)+
                          df.Year.astype(str).add('-0') ,format='%W%Y-%w')
print (df)

  Week  Year         DT
0    43  2016 2016-10-30
1    44  2016 2016-11-06
2    51  2016 2016-12-25
3     2  2017 2017-01-15
4     5  2017 2017-02-05
5    12  2017 2017-03-26
df["DT"] = pd.to_datetime(df.Week.astype(str)+
                          df.Year.astype(str).add('-1') ,format='%W%Y-%w')
print (df)
   Week  Year         DT
0    43  2016 2016-10-24
1    44  2016 2016-10-31
2    51  2016 2016-12-19
3     2  2017 2017-01-09
4     5  2017 2017-01-30
5    12  2017 2017-03-20

df["DT"] = pd.to_datetime(df.Week.astype(str)+
                          df.Year.astype(str).add('-2') ,format='%W%Y-%w')
print (df)

   Week  Year         DT
0    43  2016 2016-10-25
1    44  2016 2016-11-01
2    51  2016 2016-12-20
3     2  2017 2017-01-10
4     5  2017 2017-01-31
5    12  2017 2017-03-21

答案 1 :(得分:7)

试试这个:

In [19]: pd.to_datetime(df.Year.astype(str), format='%Y') + \
             pd.to_timedelta(df.Week.mul(7).astype(str) + ' days')
Out[19]:
0   2016-10-28
1   2016-11-04
2   2016-12-23
3   2017-01-15
4   2017-02-05
5   2017-03-26
dtype: datetime64[ns]
  

最初我在s

中有时间戳

从UNIX纪元时间戳解析它要容易得多:

df['Date'] = pd.to_datetime(df['UNIX_Time'], unit='s')
对于10M行DF

计时

设定:

In [26]: df = pd.DataFrame(pd.date_range('1970-01-01', freq='1T', periods=10**7), columns=['date'])

In [27]: df.shape
Out[27]: (10000000, 1)

In [28]: df['unix_ts'] = df['date'].astype(np.int64)//10**9

In [30]: df
Out[30]:
                       date    unix_ts
0       1970-01-01 00:00:00          0
1       1970-01-01 00:01:00         60
2       1970-01-01 00:02:00        120
3       1970-01-01 00:03:00        180
4       1970-01-01 00:04:00        240
5       1970-01-01 00:05:00        300
6       1970-01-01 00:06:00        360
7       1970-01-01 00:07:00        420
8       1970-01-01 00:08:00        480
9       1970-01-01 00:09:00        540
...                     ...        ...
9999990 1989-01-05 10:30:00  599999400
9999991 1989-01-05 10:31:00  599999460
9999992 1989-01-05 10:32:00  599999520
9999993 1989-01-05 10:33:00  599999580
9999994 1989-01-05 10:34:00  599999640
9999995 1989-01-05 10:35:00  599999700
9999996 1989-01-05 10:36:00  599999760
9999997 1989-01-05 10:37:00  599999820
9999998 1989-01-05 10:38:00  599999880
9999999 1989-01-05 10:39:00  599999940

[10000000 rows x 2 columns]

检查:

In [31]: pd.to_datetime(df.unix_ts, unit='s')
Out[31]:
0         1970-01-01 00:00:00
1         1970-01-01 00:01:00
2         1970-01-01 00:02:00
3         1970-01-01 00:03:00
4         1970-01-01 00:04:00
5         1970-01-01 00:05:00
6         1970-01-01 00:06:00
7         1970-01-01 00:07:00
8         1970-01-01 00:08:00
9         1970-01-01 00:09:00
                  ...
9999990   1989-01-05 10:30:00
9999991   1989-01-05 10:31:00
9999992   1989-01-05 10:32:00
9999993   1989-01-05 10:33:00
9999994   1989-01-05 10:34:00
9999995   1989-01-05 10:35:00
9999996   1989-01-05 10:36:00
9999997   1989-01-05 10:37:00
9999998   1989-01-05 10:38:00
9999999   1989-01-05 10:39:00
Name: unix_ts, Length: 10000000, dtype: datetime64[ns]

定时:

In [32]: %timeit pd.to_datetime(df.unix_ts, unit='s')
10 loops, best of 3: 156 ms per loop

结论:我认为转换10.000.000行的156毫秒并不是那么慢

答案 2 :(得分:3)

从2019年开始的几周中发生了一些麻烦。ISO-8601标准将2018年12月31日指定为2019年的第1周。其他方法基于:

pd.to_datetime(df.Week.astype(str)+
                  df.Year.astype(str).add('-2') ,format='%W%Y-%w')

从2019年开始,结果会有所不同。

为了符合ISO-8601标准,您必须执行以下操作:

import pandas as pd
import datetime

L1 = [52,53,1,2,5,52]
L2 = [2018,2018,2019,2019,2019,2019]
df = pd.DataFrame({"Week":L1,"Year":L2})
df['ISO'] = df['Year'].astype(str) + '-W' + df['Week'].astype(str) + '-1'
df['DT'] = df['ISO'].map(lambda x: datetime.datetime.strptime(x, "%G-W%V-%u"))
print(df)

它打印:

   Week  Year         ISO         DT
0    52  2018  2018-W52-1 2018-12-24
1    53  2018  2018-W53-1 2018-12-31
2     1  2019   2019-W1-1 2018-12-31
3     2  2019   2019-W2-1 2019-01-07
4     5  2019   2019-W5-1 2019-01-28
5    52  2019  2019-W52-1 2019-12-23

将忽略2018年的第53周并将其映射到2019年的第1周。

请在https://www.epochconverter.com/weeks/2019上进行验证。

答案 3 :(得分:1)

如果您想关注ISO Week Date

每周从星期一开始。每个星期的年份是公历年 星期四秋天。因此,一年的第一周总是 包含1月4日。因此,ISO周年编号略有不同 在1月1日前后有几天偏离公历。

以下示例代码从2016年12月18日开始,生成60个日期的序列,并添加适当的列。

它添加:

  • “日期”
  • “日期”的星期几
  • 查找该“日期”从星期一开始的一周
  • 查找从该“日期”的星期一开始的一周的年份
  • 添加星期编号(ISO)
  • 从年份和星期编号获取开始的星期一日期

下面的示例代码:

# Generate Some Dates
dft1 = pd.DataFrame(pd.date_range('2016-12-18', freq='D', periods=60))
dft1.columns = ['e_FullDate']
dft1['e_FullDateWeekDay'] = dft1.e_FullDate.dt.day_name().str.slice(0,3)


#Add a Week Start Date (Monday)
dft1['e_week_start'] = dft1['e_FullDate'] - pd.to_timedelta(dft1['e_FullDate'].dt.weekday,
                                                      unit='D')
dft1['e_week_startWeekDay'] = dft1.e_week_start.dt.day_name().str.slice(0,3)

#Add a Week Start Year
dft1['e_week_start_yr'] = dft1.e_week_start.dt.year

#Add a Week Number of Week Start Monday
dft1['e_week_no'] = dft1['e_week_start'].dt.week

#Add a Week Start generate from Week Number and Year
dft1['e_week_start_from_week_no'] = pd.to_datetime(dft1.e_week_no.astype(str)+
                  dft1.e_week_start_yr.astype(str).add('-1') ,format='%W%Y-%w')
dft1['e_week_start_from_week_noWeekDay'] = dft1.e_week_start_from_week_no.dt.day_name().str.slice(0,3)


with pd.option_context('display.max_rows', 999, 'display.max_columns', 0, 'display.max_colwidth', 9999):
    display(dft1)

enter image description here