将日期时间列转换为不同的时区 Pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34789888/
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
Convert datetime columns to a different timezone pandas
提问by trench
I have two datetime columns which are naive when I read them into memory but which are in US/Eastern actually. I simply want to convert both of these columns to US/Central.
我有两个日期时间列,当我将它们读入内存时很幼稚,但实际上它们在美国/东部。我只是想将这两列都转换为 US/Central。
I found a method which works but it seems like I am doing a workaround. I changed my call_start and call_end columns to be named 'start' and 'end' instead so I don't end up with duplicate column names. I then created a separate datetimeindex for each of these columns and reset the index.
我找到了一种有效的方法,但似乎我正在做一个解决方法。我将 call_start 和 call_end 列改为命名为“start”和“end”,这样我就不会得到重复的列名。然后我为这些列中的每一列创建了一个单独的日期时间索引并重置索引。
aht.set_index(pd.DatetimeIndex(aht['start']).tz_localize('US/Eastern').tz_convert('US/Central'), inplace = True, drop = True)
aht.index.names = ['call_start']
aht = aht.reset_index()
aht.set_index(pd.DatetimeIndex(aht['end']).tz_localize('US/Eastern').tz_convert('US/Central'), inplace = True, drop = True)
aht.index.names = ['call_end']
aht = aht.reset_index()
I end up getting:
我最终得到:
call_end call_start start end
2016-01-13 06:05:01-06:00 2016-01-13 06:02:00-06:00 01/13/2016 07:02 01/13/2016 07:05
2016-01-13 06:07:00-06:00 2016-01-13 06:03:16-06:00 01/13/2016 07:03 01/13/2016 07:07
2016-01-13 06:09:13-06:00 2016-01-13 06:06:02-06:00 01/13/2016 07:06 01/13/2016 07:09
2016-01-13 06:17:51-06:00 2016-01-13 06:06:20-06:00 01/13/2016 07:06 01/13/2016 07:17
Is this the best method? All other data is in central time so I just want to make sure that this file is too so when I merge files together it makes more sense. I do not care about having the actual timezone stamp there though - is there a way to easily strip it after I created my new columns?
这是最好的方法吗?所有其他数据都在中心时间,所以我只想确保这个文件也是如此,所以当我将文件合并在一起时它更有意义。我不关心在那里有实际的时区戳 - 有没有办法在我创建新列后轻松剥离它?
回答by joris
You don't need to do the roundtrip to DatetimeIndex, as these methods are avaliable for a Series (column) as well through the dt
accessor:
您不需要对 DatetimeIndex 进行往返,因为这些方法也可通过dt
访问器用于系列(列):
aht['call_start'] = aht['start'].dt.tz_localize('US/Eastern').dt.tz_convert('US/Central')
And the same for end
.
To remove the timezone information but keep it in the local time, you do another .dt.tz_localize(None)
afterwards (see this question: https://stackoverflow.com/a/34687479/653364)
对于end
.
要删除时区信息但将其保留在本地时间,请.dt.tz_localize(None)
稍后再执行一次(请参阅此问题:https: //stackoverflow.com/a/34687479/653364)