添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
当我打印DF的 df.dtypes 时,我得到的是 "对象 "类型,而不是 "日期时间"。

我有数据框架。

| Resource | timestamp                       |
| -------- | --------------                  |
| 112      | 2011-10-01 00:38:44.546000+02:00|
| 113      | 2011-10-02 00:38:44.546000+02:00|
| 112      | 2011-10-03 00:38:44.546000+02:00|
| 115      | 2011-10-04 00:38:44.546000+02:00|
| 114      | 2011-10-05 00:38:44.546000+02:00|

I used

df_log['time:timestamp'] = df_log['time:timestamp'].apply(lambda x: dt.strftime(dt.fromisoformat(str(x)),'%Y-%m-%d.%H:%M:%S'))

它去掉了'+'号,给了我日期和时间部分,但我觉得我做错了,因为我没有很好的数据时间处理知识!我的目标是只得到日期和时间,比如 "2011-10-01 00:38:44",而且类型是 "datetime "而不是 "object"。 我的目标是只得到日期和时间,比如 "2011-10-01 00:38:44",并且类型是 "datetime",而不是我现在得到的对象。

4 个评论
我没有得到对象。在做完df['timestamp'] = pd.to_datetime(df['timestamp'])后,我得到了datetime64[ns, pytz.FixedOffset(120)]的dtype。
How about pd.to_datetime(df['timestamp'].astype(str).str[:19])
@Ferris 谢谢,你的代码比我的短且干净,尽管两者的结果相同。我的问题是,我是否做错了,因为我正在努力学习和理解数据时间转换过程。
@DavidErickson 我的表是从一个日志文件(xes)转换到pandas df的 - 如果它最初是df,那么你是对的,它将产生datetime类型。
python
pandas
datetime
Sam.H
Sam.H
发布于 2021-01-05
3 个回答
David Erickson
David Erickson
发布于 2022-02-13
已采纳
0 人赞同

你可以使用。

df['timestamp'] = pd.to_datetime(df['timestamp'].str.replace('\+.*', ''))
df.info()
     #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Resource   5 non-null      object        
 1   timestamp  5 non-null      datetime64[ns]
df['timestamp'] = pd.to_datetime(df['timestamp'])
df.info()

如果没有替换,你会返回一个datetime64[ns, pytz.FixedOffset(120)]的日期类型。

df['timestamp'] = pd.to_datetime(df['timestamp'])
df.info()
     #   Column     Non-Null Count  Dtype                                
---  ------     --------------  -----                                
 0   Resource   5 non-null      object                               
 1   timestamp  5 non-null      datetime64[ns, pytz.FixedOffset(120)]

你也可以使用。

df['timestamp'] = pd.to_datetime(df['timestamp']).dt.tz_convert(None)

从这个答案来看。https://stackoverflow.com/a/59919896/6366770

Per the docs,在2019年1月25日发布的0.24.0中,pandas处理datetime偏移的方式有了变化。

*Previous behavior:
In [2]: pd.to_datetime("2015-11-18 15:30:00+05:30")
Out[2]: Timestamp('2015-11-18 10:00:00')
In [3]: pd.Timestamp("2015-11-18 15:30:00+05:30")
Out[3]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')
# Different UTC offsets would automatically convert the datetimes to UTC (without a UTC timezone)
In [4]: pd.to_datetime(["2015-11-18 15:30:00+05:30", "2015-11-18 16:30:00+06:30"])
Out[4]: DatetimeIndex(['2015-11-18 10:00:00', '2015-11-18 10:00:00'], dtype='datetime64[ns]', freq=None)
New behavior:
In [56]: pd.to_datetime("2015-11-18 15:30:00+05:30")
Out[56]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')
In [57]: pd.Timestamp("2015-11-18 15:30:00+05:30")
Out[57]: Timestamp('2015-11-18 15:30:00+0530', tz='pytz.FixedOffset(330)')*
    
谢谢David的回答。我在做替换代码时得到了AttributeError: df['timestamp'] = pd.to_datetime(df['timestamp'].str.replace('\+.*', '')) "AttributeError:只能对字符串值使用.str访问器!"
你的代码dt.tz_convert(None)工作得很好。我必须先将该列转换为UTC=True的日期类型,然后应用你的tz转换,我得到了正确的时间(是-2小时,即2011-10-01 00:38:44给我的是2011-09-30 22:38:44)。
Juan Camilo Rivera Palacio
Juan Camilo Rivera Palacio
发布于 2022-02-13
0 人赞同

在你的那行代码之后。试一下。

df_log['time:timestamp'] = datetime.strptime(df_log['time:timestamp'], '%Y-%m-%d.%H:%M:%S')
    
Phoenix
Phoenix
发布于 2022-02-13
0 人赞同

花了一些时间后,这些线条解决了我的问题。

  • 将时区应用于该列并将时间戳转换为日期和时间。

    df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True).dt.tz_convert(None)
    
  • 移除时区而不应用于该列。(我的最爱)

    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.replace(tzinfo=None))
    
  •