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

查看:111
本文介绍了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

幸运的是,我已将带有 dates 的数据框保存在 csv 中,但我现在想以 23:00:00.051 格式创建另一列 time

Luckily, I have saved the data frame with dates in the csv but I now want to create another column time in the format 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 变为对象.现在我该如何解决这个问题?

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?

推荐答案

先这样做:

df['time'] = pd.to_datetime(df['timestamp'])

在您像往常一样进行提取之前:

Before you do your extraction as usual:

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

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

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