如何在熊猫数据框中将异常时间戳转换为日期时间

时间:2019-01-29 23:05:32

标签: python pandas heatmap

我正在为某些用户分析创建使用情况热图。 Y轴为星期几,X轴为一天中的小时(24:00)。我从API中提取了数据。(请注意,这实际上会产生6,000行数据)

IN:

import requests
import json

response = requests.get("api.url")
data = response.json()
df=pd.DataFrame(data['Sessions'])
df.dtypes
print(df['StartTime'])

OUT:

0     2019-01-29T22:08:40
1     2019-01-29T22:08:02
2     2019-01-29T22:05:10
3     2019-01-29T21:34:30
4     2019-01-29T21:32:49
Name: StartTime, Length: 100, dtype: object

我通常会将对象转换为pandas.dt,然后将其分为两列:

IN:

df['StartTime'] =  pd.to_datetime(df['StartTime'], format='%d%b%Y:%H:%M:%S.%f')
df['Date'] = [d.date() for d in df['StartTime']]
df['Time'] = [d.time() for d in df['StartTime']]

OUT:

'     StartTime                Date           Time
0     2019-01-29T22:08:40      2019-01-29     22:08:40
1     2019-01-29T22:08:02      2019-01-29     22:08:02
2     2019-01-29T22:05:10      2019-01-29     22:05:10
3     2019-01-29T21:34:30      2019-01-29     21:34:30
4     2019-01-29T21:32:49      2019-01-29     21:32:49

由于我的时间戳记中间有一个时髦的“ T”,可能是因为数据类型,所以此方法不起作用。

我需要删除T,以便可以将其转换为标准的日期时间格式,然后需要将“日期”和“时间”分成各自的列。奖金:我只想把一个小时带到自己的专栏中。而不是22:08:02,而是22。

3 个答案:

答案 0 :(得分:0)

您需要使用熊猫时间戳:

private aes256: AES256

所以:

>>> pd.Timestamp(‘2017-01-01T12’)
Timestamp(‘2017-01-01 12:00:00’)

如@coldspeed所述,调用pd.to_datetime()或pd.Timesatmp()可以正常工作,只需省略df['StartTime'] = df["StartTime"].apply(lambda x: pd.Timestamp(x)) #now StartTime has the correct data type so you can access # date and time methods as well as the hour df['Date'] = df["StartTime"].apply(lambda x: x.date()) df['Time'] = df["StartTime"].apply(lambda x: x.time()) df['Hour'] = df["StartTime"].apply(lambda x: x.hour) 参数

答案 1 :(得分:0)

解析时间戳dateutil非常棒。它几乎可以从任何字符串格式中找出日期。

要仅从日期时间对象中获取小时,可以使用 d.hour

答案 2 :(得分:0)

您不需要格式化时间戳记。熊猫可以识别日期时间格式,例如“ 2019-01-29T21:34:30”。

IN:

import pandas as pd    
dt = '2019-01-29T21:34:30'    
pd.to_datetime(dt)

OUT:

Timestamp('2019-01-29 21:11:15')