gpt4 book ai didi

python - Pandas 无法从 Numpy 时间戳数组创建 DataFrame

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:01 24 4
gpt4 key购买 nike

我有一个 Pandas 时间戳的 numpy 数组:

array([[Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')],
[Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')],
[Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object)

我无法从此数组创建 DataFrame,因为尝试这样做会引发以下错误:

AssertionError: Number of Block dimensions (1) must equal number of axes (2)

您可以看到该数组显然是二维的,我使用 ndim 验证了这一点。

为什么我无法创建 DataFrame?

最佳答案

我认为你可以使用列表理解:

import pandas as pd
import numpy as np

a =np.array([[pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')],
[pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')],
[pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'),
pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object)

df = pd.DataFrame([x for x in a], columns=['a','b','c'])
print (df)
a b \
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00

c
0 2016-05-02 15:50:00+00:00
1 2016-05-02 17:10:00+00:00
2 2016-05-02 20:25:00+00:00

另一个解决方案是 DataFrame.from_records :

print (pd.DataFrame.from_records(a, columns=['a','b','c']))
a b \
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00

c
0 2016-05-02 15:50:00+00:00
1 2016-05-02 17:10:00+00:00
2 2016-05-02 20:25:00+00:00

参见alternate constructors of df .

关于python - Pandas 无法从 Numpy 时间戳数组创建 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com