大 pandas :从时间戳中提取日期和时间 [英] pandas: extract date and time from timestamp

查看:255
本文介绍了大 pandas :从时间戳中提取日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个timestamp列,其中时间戳采用以下格式

I have a timestamp column where the timestamp is in the following format

2016-06-16T21:35:17.098+01:00

我想从中提取日期和时间.我已完成以下操作:

I want to extract date and time from it. I have done the following:

import datetime as dt

df['timestamp'] = df['timestamp'].apply(lambda x : pd.to_datetime(str(x)))

df['dates'] = df['timestamp'].dt.date

这工作了一段时间.但是突然没有.

This worked for a while. But suddenly it does not.

如果再次执行df['dates'] = df['timestamp'].dt.date,我会收到以下错误

If I again do df['dates'] = df['timestamp'].dt.date I get the following error

Can only use .dt accessor with datetimelike values

幸运的是,我已经在中将数据框保存为csv,但现在我想以23:00:00.051

编辑

在原始数据文件(1500万个样本)中,timestamp列如下所示(前5个样本):

From the raw data file (15 million samples), the timestamp column looks like following (first 5 samples):

            timestamp

0           2016-06-13T00:00:00.051+01:00
1           2016-06-13T00:00:00.718+01:00
2           2016-06-13T00:00:00.985+01:00
3           2016-06-13T00:00:02.431+01:00
4           2016-06-13T00:00:02.737+01:00

以下命令之后

df['timestamp'] = df['timestamp'].apply(lambda x : pd.to_datetime(str(x)))

timestamp列的外观类似于dtype作为dtype:datetime64 [ns]

the timestamp column looks like with dtype as dtype: datetime64[ns]

0    2016-06-12 23:00:00.051
1    2016-06-12 23:00:00.718
2    2016-06-12 23:00:00.985
3    2016-06-12 23:00:02.431
4    2016-06-12 23:00:02.737

然后终于

df['dates'] = df['timestamp'].dt.date

0           2016-06-12
1           2016-06-12
2           2016-06-12
3           2016-06-12
4           2016-06-12

编辑2

发现了错误.我已经清理了数据并将数据帧保存在一个csv文件中,因此不必再次清理.当我读取csv时,时间戳记dtype更改为object.现在如何解决这个问题?

Found the mistake. I had cleaned the data and saved the data frame in a csv file, so I don't have to do the cleaning again. When I read the csv, the timestamp dtype changes to object. Now how do I fix this?

推荐答案

如果日期为字符串形式,则:

If date is in string form then:

import datetime

# this line converts the string object in Timestamp object
df['DateTime'] = [datetime.datetime.strptime(d, "%Y-%m-%d %H:%M") for d in df["DateTime"]]

# extracting date from timestamp
df['Date'] = [datetime.datetime.date(d) for d in df['DateTime']] 

# extracting time from timestamp
df['Time'] = [datetime.datetime.time(d) for d in df['DateTime']] 

如果对象已经是时间戳格式,则跳过第一行代码.

If the object is already in the Timestamp format then skip the first line of code.

%Y-%m-%d %H:%M,这意味着您的时间戳记对象必须采用类似于2016-05-16 12:35:00的形式.

%Y-%m-%d %H:%M this means your timestamp object must be in the form like 2016-05-16 12:35:00.

这篇关于大 pandas :从时间戳中提取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆