如何将 pandas 中的日期时间列全部转换为相同的时区 [英] How can I convert my datetime column in pandas all to the same timezone

查看:57
本文介绍了如何将 pandas 中的日期时间列全部转换为相同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 DataTime 列的数据框(带有不同格式的时区).时区似乎是 UTC,但我想将该列转换为 pd.to_datetime 并且失败了.那是问题#1.由于失败,我无法对时间段进行任何日期时间操作,例如按日期对列进行分组/计算天数/按一天中的小时分组等等.这是我的数据框 df_res

I have a dataframe with a DataTime column (with Timezone in different formats). It appears like timezone is UTC but I want to convert the column to pd.to_datetime and that is failing. That is problem #1. Since that fails I cannot do any datetime operations on the time period such as group the column by date / figure out the days / group by hour of the day and so on. Here's my dataframe df_res

    DateTime
    2017-11-02 19:49:28-07:00
    2017-11-27 07:32:22-08:00
    2017-12-27 17:01:15-08:00

命令的输出

      df_res["DateTime"] = df_res["DateTime"].dt.tz_convert('America/New_York')

AttributeError: Can only use .dt accessor with datetimelike values

当我转换为 datetime

   df_res['DateTime'] = pd.to_datetime(df_res['DateTime'])

ValueError: Tz-aware datetime.datetime 不能转换为 datetime64,除非 utc=True

我觉得我在绕圈子.我需要将列转换为日期时间才能执行操作&为了做到这一点,我需要让它们都在相同的时区,但我不能有相同的时区,除非它是一个日期时间对象,所以我怎样才能最好地解决这个问题.我确实参考了以前的帖子,但它们似乎尽可能轻松地转换为日期时间:

I feel I am going around in circles. I need to convert the column to datetime in order to perform operations & in order to do that I need to have them all the same timezone but I cannot have the same timezone unless it is a datetime object so how can I best approach this. I did refer to previous postings but they seem to convert to datetime as easily as possible:

将日期时间列转换为不同时区的熊猫转换熊猫时区-知道 DateTimeIndex 到朴素的时间戳,但在某些时区

推荐答案

我认为没有必要应用 lambdas:

I think that it is not necessary to apply lambdas:

df_res['DateTime'] = pd.to_datetime(df_res['DateTime'], utc=True)

文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

这篇关于如何将 pandas 中的日期时间列全部转换为相同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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