pandas: extract date and time from timestamp(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:从时间戳中提取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:pandas:从时间戳中提取日期和时间


- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 沿轴计算直方图 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python-m http.server 443--使用SSL? 2022-01-01