【问题标题】:Remove timezone (+01:00) from DateTime从 DateTime 中删除时区 (+01:00)
【发布时间】:2020-02-24 04:39:15
【问题描述】:

我想从我的 dateTime 对象中删除时区。
目前我有:
2019-02-21 15:31:37+01:00

预期输出:
2019-02-21 15:31:37

我将其转换为的代码:2019-02-21 14:31:37。

# Convert to date
mood['response_time'] = pd.to_datetime(mood['response_time'], utc=True)
# Remove +01:00
mood['response_time'] = mood['response_time'].dt.strftime('%Y-%m-%d %H:%M:%S')

【问题讨论】:

  • 所以您只是想直观地剥离 TZ 信息,或者您想转换为天真的日期时间,或者您想保留时区?当你做这样的事情时,事情会在时间上变得有点模棱两可。
  • 只是直观地剥离 TZ 信息,谢谢。
  • 已经试过了。这:mood = mood['response_time'].replace(tzinfo=None)。返回:TypeError: replace() got an unexpected keyword argument 'tzinfo'
  • 让用户心情["response_time"] 首先是一个日期时间对象。

标签: python pandas datetime


【解决方案1】:

在第一行中,参数 utc=True 不是必需的,因为它将输入转换为 UTC(在您的情况下减去一小时)。

在第二行,我得到一个AttributeError: 'Timestamp' object has no attribute 'dt'。请注意 to_datetime 可以 return different objects 取决于输入。

所以以下对我有用(使用Timestamp 对象):

mood['response_time'] = '2019-02-21 15:31:37+01:00'
# Convert to date
mood['response_time'] = pd.to_datetime(mood['response_time'])
# Remove +01:00
mood['response_time'] = mood['response_time'].strftime('%Y-%m-%d %H:%M:%S')
# -> '2019-02-21 15:31:37'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多