rag*_*esz 24 python datetime timestamp pandas
我有一个包含一些(数百)百万行的数据帧.我想有效地将datetime转换为时间戳.我该怎么做?
我的样本df:
df = pd.DataFrame(index=pd.DatetimeIndex(start=dt.datetime(2016,1,1,0,0,1),
end=dt.datetime(2016,1,2,0,0,1), freq='H'))\
.reset_index().rename(columns={'index':'datetime'})
df.head()
datetime
0 2016-01-01 00:00:01
1 2016-01-01 01:00:01
2 2016-01-01 02:00:01
3 2016-01-01 03:00:01
4 2016-01-01 04:00:01
Run Code Online (Sandbox Code Playgroud)
现在我将datetime转换为timestamp value-by-value .apply()但是如果我有一些(几百万)行,则需要很长时间(几个小时):
df['ts'] = df[['datetime']].apply(lambda x: x[0].timestamp(), axis=1).astype(int)
df.head()
datetime ts
0 2016-01-01 00:00:01 1451602801
1 2016-01-01 01:00:01 1451606401
2 2016-01-01 02:00:01 1451610001
3 2016-01-01 03:00:01 1451613601
4 2016-01-01 04:00:01 1451617201
Run Code Online (Sandbox Code Playgroud)
以上结果就是我想要的.
如果我尝试使用当时的.dt访问器,pandas.Series我会收到错误消息:
df['ts'] = df['datetime'].dt.timestamp
Run Code Online (Sandbox Code Playgroud)
AttributeError:'DatetimeProperties'对象没有属性'timestamp'
如果我尝试创建例如.使用访问器的日期时间的日期部分.dt然后使用它更快.apply()!
df['date'] = df['datetime'].dt.date
df.head()
datetime ts date
0 2016-01-01 00:00:01 1451602801 2016-01-01
1 2016-01-01 01:00:01 1451606401 2016-01-01
2 2016-01-01 02:00:01 1451610001 2016-01-01
3 2016-01-01 03:00:01 1451613601 2016-01-01
4 2016-01-01 04:00:01 1451617201 2016-01-01
Run Code Online (Sandbox Code Playgroud)
我想要一些类似于时间戳的东西......
但我真的不明白官方文档:它谈到" 转换为时间戳 "但我没有看到任何时间戳; 它只谈到转换到datetime pd.to_datetime()而不是时间戳...
pandas.Timestamp 构造函数也不起作用(返回以下错误):
df['ts2'] = pd.Timestamp(df['datetime'])
Run Code Online (Sandbox Code Playgroud)
TypeError:无法将输入转换为Timestamp
pandas.Series.to_timestamp 也会让我想要的东西完全不同:
df['ts3'] = df['datetime'].to_timestamp
df.head()
datetime ts ts3
0 2016-01-01 00:00:01 1451602801 <bound method Series.to_timestamp of 0 2016...
1 2016-01-01 01:00:01 1451606401 <bound method Series.to_timestamp of 0 2016...
2 2016-01-01 02:00:01 1451610001 <bound method Series.to_timestamp of 0 2016...
3 2016-01-01 03:00:01 1451613601 <bound method Series.to_timestamp of 0 2016...
4 2016-01-01 04:00:01 1451617201 <bound method Series.to_timestamp of 0 2016...
Run Code Online (Sandbox Code Playgroud)
谢谢!!
jez*_*ael 28
我认为你需要先转换为numpy arrayby values并转换为int64- 输出是ns,所以需要除以10 ** 9:
df['ts'] = df.datetime.values.astype(np.int64) // 10 ** 9
print (df)
datetime ts
0 2016-01-01 00:00:01 1451606401
1 2016-01-01 01:00:01 1451610001
2 2016-01-01 02:00:01 1451613601
3 2016-01-01 03:00:01 1451617201
4 2016-01-01 04:00:01 1451620801
5 2016-01-01 05:00:01 1451624401
6 2016-01-01 06:00:01 1451628001
7 2016-01-01 07:00:01 1451631601
8 2016-01-01 08:00:01 1451635201
9 2016-01-01 09:00:01 1451638801
10 2016-01-01 10:00:01 1451642401
11 2016-01-01 11:00:01 1451646001
12 2016-01-01 12:00:01 1451649601
13 2016-01-01 13:00:01 1451653201
14 2016-01-01 14:00:01 1451656801
15 2016-01-01 15:00:01 1451660401
16 2016-01-01 16:00:01 1451664001
17 2016-01-01 17:00:01 1451667601
18 2016-01-01 18:00:01 1451671201
19 2016-01-01 19:00:01 1451674801
20 2016-01-01 20:00:01 1451678401
21 2016-01-01 21:00:01 1451682001
22 2016-01-01 22:00:01 1451685601
23 2016-01-01 23:00:01 1451689201
24 2016-01-02 00:00:01 1451692801
Run Code Online (Sandbox Code Playgroud)
Mit*_*ril 15
我认为你不应该使用申请,astype就可以了:
df['ts'] = df.datetime.astype('int64') // 10**9
Run Code Online (Sandbox Code Playgroud)
如果您不想使用 numpy,则可以使用纯 Pandas 转换
df['ts'] = pd.to_timedelta(df['datetime'], unit='ns').dt.total_seconds().astype(int)
Run Code Online (Sandbox Code Playgroud)