将 Pandas DF 列从 UTC 转换为不带日期的美国东部时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35398944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:41:37  来源:igfitidea点击:

Convert Pandas DF column from UTC to US-Eastern time without date

python-2.7pandastimezone

提问by edesz

I have this Pandas dataframe column:

我有这个 Pandas 数据框列:

                     time_UTC
0   2015-01-05 16:44:34+00:00
1   2015-08-11 16:44:38+00:00
2   2015-08-02 16:53:25+00:00
3   2015-08-17 16:53:25+00:00
4   2015-09-28 16:53:26+00:00
Name: time_UTC, dtype: datetime64[ns, UTC]

and I converted it from UTC to US-Eastern timezone using:

我使用以下方法将其从 UTC 转换为美国东部时区:

list_temp = []
    for row in df['time_UTC']:
        list_temp.append(Timestamp(row, tz = 'UTC').tz_convert('US/Eastern'))
    df['time_EST'] = list_temp

to get this:

得到这个:

0   2015-01-05 11:44:34-05:00
1   2015-08-11 11:44:38-05:00
2   2015-08-02 11:53:25-05:00
3   2015-08-17 11:53:25-05:00
4   2015-09-28 11:53:26-05:00
Name: time_EST, dtype: datetime64[ns, US/Eastern]

Now, I need to drop the date part of the entries so that I only get the time. Here is what I need:

现在,我需要删除条目的日期部分,以便我只获取时间。这是我需要的:

0   11:44:34-05:00
1   11:44:38-05:00
2   11:53:25-05:00
3   11:53:25-05:00
4   11:53:26-05:00
Name: time_EST, dtype: datetime64[ns, US/Eastern]

Attempt:

试图:

I tried this:

我试过这个:

print df['time_EST'].apply(lambda x: dt.time(x.hour,x.minute,x.second))

The conversion is made so that date is dropped and I only get time. But it is reverting back to the UTC timezone. Here is the output of the above command:

进行转换以便删除日期并且我只有时间。但它正在恢复到 UTC 时区。以下是上述命令的输出:

0    16:44:34
1    16:44:38
2    16:53:25
3    16:53:25
4    16:53:26
Name: time_EST, dtype: object

Question:

题:

Is there a way to drop the date and keep time as US-Eastern, without automatically reverting back to UTC?

有没有办法删除日期并将时间保持为美国东部时间,而不会自动恢复为 UTC?

EDIT:

编辑:

To recreate the problem, just copy the first DataFrame above and use this code:

要重现该问题,只需复制上面的第一个 DataFrame 并使用以下代码:

import pandas as pd
from pandas.lib import Timestamp
import datetime as dt
df = pd.read_clipboard()

Then copy the remaining lines of code from the question. Any assistance with this problem would be greatly appreciated.

然后复制问题中剩余的代码行。对这个问题的任何帮助将不胜感激。

回答by maxymoo

You want to use strftimeto format your string, also note the vectorized date manipulations:

您想使用strftime格式化字符串,还要注意矢量化日期操作:

df = pd.read_clipboard()
df.time_UTC = pd.to_datetime(df.time_UTC)
df['EST'] = (df.time_UTC.dt.tz_localize('UTC')
                        .tz_convert('US/Eastern')
                        .strftime("%H:%M:%S"))

In [41]: df
Out[41]:
                               time_UTC       EST
time_UTC
2016-02-15 16:44:34 2016-02-15 16:44:34  11:44:34
2016-02-15 16:44:38 2016-02-15 16:44:38  11:44:38
2016-02-15 16:53:25 2016-02-15 16:53:25  11:53:25
2016-02-15 16:53:25 2016-02-15 16:53:25  11:53:25
2016-02-15 16:53:26 2016-02-15 16:53:26  11:53:26