简体   繁体   English

将带有时间戳字符串的Pandas DF转换为不带tz的本地日期时间

[英]Convert Pandas DF with timestamp string to local datetime without tz

I have DataFrame column with string timestamp and I want to convert it to local datetime without time zone. 我有带字符串时间戳的DataFrame列,我想将其转换为不带时区的本地日期时间。

df = {'id' : [1, 2, 3],
      'timestamp' : ['2019-07-01T21:30:20Z', '2019-07-02T21:30:20Z', '2019-07-03T21:30:20Z']}
df = pd.DataFrame(df, columns = ['id','timestamp'])

My code: 我的代码:

df['timestamp'] = (pd.DatetimeIndex(pd.to_datetime(df['timestamp'], format="%Y-%m-%dT%H:%M:%SZ",
  errors='ignore')).tz_localize('UTC').tz_convert('Europe/Prague'))

This works for example above but not for example below. 例如,这在上面有效,但在下面不起作用。 OutOfBoundsDatetime: Out of bounds nanosecond timestamp OutOfBoundsDatetime:超出范围的纳秒级时间戳

df = {'id' : [1, 2, 3],
      'timestamp' : ['2019-07-01T21:30:20Z', '2999-12-31T21:30:20Z', '9999-12-30T21:30:20Z']}

It is problem with 2999-12-31T21:30:20Z or 9999-12-30T21:30:20Z. 2999-12-31T21:30:20Z或9999-12-30T21:30:20Z有问题。 How can I solve it? 我该如何解决?

So the max timestamp pandas can hold is '2262-04-11 23:47:16.854775807' (see https://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.html#timestamp-limitations ). 因此熊猫可以容纳的最大时间戳为'2262-04-11 23:47:16.854775807' (请参阅https://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.html#timestamp-limitations ) 。 Therefore both the second and third timestamps are the issues. 因此,第二个和第三个时间戳都是问题。

As for the fix if you need those dates I am unsure if I can help you. 至于解决方案,如果您需要这些日期,我不确定是否可以帮助您。 However you can drop them by changing the errors parameter. 但是,您可以通过更改errors参数来删除它们。

I also got rid of some of the functions you called as they don't need to be there. 我还摆脱了您调用的某些函数,因为它们不需要在那里。

df = pd.DataFrame({'id' : [1, 2, 3], 
                   'timestamp' : ['2019-07-01T21:30:20Z', 
                                  '2999-12-31T21:30:20Z', 
                                  '9999-12-30T21:30:20Z']})

df['timestamp'] = pd.to_datetime(df['timestamp'], 
                                 format="%Y-%m-%dT%H:%M:%SZ", errors='coerce', utc=True)

df['timestamp'] = df['timestamp'].dt.tz_convert('Europe/Prague')

df.dropna() if you need to drop the NaT values df.dropna()如果需要删除NaT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM