简体   繁体   English

如何将pandas timezone aware timestamps转换为UNIX epoche?

[英]How to convert pandas timezone aware timestamps to UNIX epoche?

I need to convert a timezone aware date_range (TimeStamps) to UNIX epoch values for use in an external Javascript library. 我需要将时区感知date_range(TimeStamps)转换为UNIX纪元值,以便在外部Javascript库中使用。

My approach is: 我的方法是:

# Create localized test data for one day
rng = pd.date_range('1.1.2014', freq='H', periods=24, tz="Europe/Berlin")
val = np.random.randn(24)
df = pd.DataFrame(data=val, index=rng, columns=['values'])

# Reset index as df column
df = df.reset_index()

# Convert the index column to the desired UNIX epoch format
df['index'] = df['index'].apply(lambda x: x.value // 10**6 )

df['index'] contains the UNIX epoch values as expected but they are are stored in UTC(!). df ['index']包含预期的UNIX纪元值,但它们以UTC(!)存储。

I suppose this is because pandas stores timestamps in numpy UTC datetime64 values under the hood. 我想这是因为pandas将时间戳存储在引擎盖下的numpy UTC datetime64值中。

Is there a smart way to get "right" epoch values in the requested time zone? 有没有一种聪明的方法可以在要求的时区内获得“正确”的纪元价值?

This proposal doesn't work with DST 此提案不适用于DST

In [17]: df
Out[17]: 
                             values
2014-01-01 00:00:00+01:00  1.027799
2014-01-01 01:00:00+01:00  1.579586
2014-01-01 02:00:00+01:00  0.202947
2014-01-01 03:00:00+01:00 -0.214921
2014-01-01 04:00:00+01:00  0.021499
2014-01-01 05:00:00+01:00 -1.368302
2014-01-01 06:00:00+01:00 -0.261738

2014-01-01 22:00:00+01:00  0.808506
2014-01-01 23:00:00+01:00  0.459895

[24 rows x 1 columns]

Use the index method asi8 to convert to int64 (which is already in ns since epoch) These are the UTC times! 使用索引方法asi8转换为int64(自纪元以来已经在ns )这些是UTC时间!

In [18]: df.index.asi8//10**6
Out[18]: 
array([1388530800000, 1388534400000, 1388538000000, 1388541600000,
       1388545200000, 1388548800000, 1388552400000, 1388556000000,
       1388559600000, 1388563200000, 1388566800000, 1388570400000,
       1388574000000, 1388577600000, 1388581200000, 1388584800000,
       1388588400000, 1388592000000, 1388595600000, 1388599200000,
       1388602800000, 1388606400000, 1388610000000, 1388613600000])

These are the local timezone since epoch. 这些是自纪元以来的当地时区。 Note that this is NOT a public method for normally, I would always exchange UTC data (and the timezone if you need). 请注意,这通常不是公共方法,我总是会交换UTC数据(如果需要,还可以交换时区)。

In [7]: df.index._local_timestamps()//10**6
Out[7]: 
array([1388534400000, 1388538000000, 1388541600000, 1388545200000,
       1388548800000, 1388552400000, 1388556000000, 1388559600000,
       1388563200000, 1388566800000, 1388570400000, 1388574000000,
       1388577600000, 1388581200000, 1388584800000, 1388588400000,
       1388592000000, 1388595600000, 1388599200000, 1388602800000,
       1388606400000, 1388610000000, 1388613600000, 1388617200000])

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

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