将dataframe index指定为创建时的日期

时间:2016-01-27 14:43:32

标签: python pandas

我是来自R背景的Python(3.5)的新手,我正在努力解决数据帧创建和使用方式的差异。特别是我想使用索引的一系列日期创建数据框。以下实验代码(请注意注释掉的index)或多或少地符合我的预期:

import pandas as pd
import numpy as np

np.random.seed(123456)
num_periods=5
monthindex=pd.date_range('1/1/2014', periods=num_periods, freq='MS')
dd = pd.DataFrame(data={'date':monthindex,
                        'c1': pd.Series(np.random.uniform(10, 20, size=num_periods)),
                        'c2': pd.Series(np.random.uniform(30, 40, size=num_periods))},
                  # index=monthindex,
)
print(dd)

...并告诉我这个输出:

          c1         c2       date
0  11.269698  33.362217 2014-01-01
1  19.667178  34.513765 2014-02-01
2  12.604760  38.402551 2014-03-01
3  18.972365  31.231021 2014-04-01
4  13.767497  35.430262 2014-05-01

...我可以在创建后指定索引,如下所示:

dd.index = monthindex
print(dd)

...这让我知道了,看起来是对的:

                   c1         c2       date
2014-01-01  11.269698  33.362217 2014-01-01
2014-02-01  19.667178  34.513765 2014-02-01
2014-03-01  12.604760  38.402551 2014-03-01
2014-04-01  18.972365  31.231021 2014-04-01
2014-05-01  13.767497  35.430262 2014-05-01

但是,如果我在上面的代码中取消注释index调用,我会在索引中获取日期,但我留下Na值,如下所示:

            c1  c2       date
2014-01-01 NaN NaN 2014-01-01
2014-02-01 NaN NaN 2014-02-01
2014-03-01 NaN NaN 2014-03-01
2014-04-01 NaN NaN 2014-04-01
2014-05-01 NaN NaN 2014-05-01

我怀疑这可能是因为两个Series对象没有与索引共享任何值,但我真的不明白发生了什么。

发生了什么以及如何在创建数据框期间指定日期索引,而不是在调用DataFrame之后添加日期索引?

2 个答案:

答案 0 :(得分:1)

您的错误在于,通过传递Series作为数据类型,您可以有效地重新编制df索引以使用这些索引并且它尝试与它们对齐,如果您只使用values然后它起作用:

In [61]:
np.random.seed(123456)
num_periods=5
monthindex=pd.date_range('1/1/2014', periods=num_periods, freq='MS')
dd = pd.DataFrame(data={'date':monthindex,
                        'c1': pd.Series(np.random.uniform(10, 20, size=num_periods)).values,
                        'c2': pd.Series(np.random.uniform(30, 40, size=num_periods)).values},
                   index=monthindex,
)
dd

Out[61]:
                   c1         c2       date
2014-01-01  11.269698  33.362217 2014-01-01
2014-02-01  19.667178  34.513765 2014-02-01
2014-03-01  12.604760  38.402551 2014-03-01
2014-04-01  18.972365  31.231021 2014-04-01
2014-05-01  13.767497  35.430262 2014-05-01

如果您比较monthindex数据与Series之间的差异:

In [60]:
monthindex

Out[60]:
DatetimeIndex(['2014-01-01', '2014-02-01', '2014-03-01', '2014-04-01',
               '2014-05-01'],
              dtype='datetime64[ns]', freq='MS')

In [59]:
pd.Series(np.random.uniform(10, 20, size=num_periods))

Out[59]:
0    13.730122
1    14.479968
2    11.294407
3    18.598787
4    18.203884
dtype: float64

您可以看到Series类型构造了默认索引,这就是您在这些列中获得NaN的原因,而如果您访问.values属性以返回np数组你得到一个没有索引的扁平数组:

In [62]:
pd.Series(np.random.uniform(10, 20, size=num_periods)).values

Out[62]:
array([ 13.73012225,  14.47996825,  11.2944068 ,  18.59878707,  18.20388363])

这顺便说一下是设计

答案 1 :(得分:1)

直接使用NumPy数组而不创建系列第一作品:

import pandas as pd
import numpy as np

np.random.seed(123456)
num_periods=5
monthindex=pd.date_range('1/1/2014', periods=num_periods, freq='MS')
dd = pd.DataFrame(data={'date':monthindex,
                        'c1': np.random.uniform(10, 20, size=num_periods),
                        'c2': np.random.uniform(30, 40, size=num_periods)},
                  index=monthindex,
)
print(dd)

输出:

                   c1         c2       date
2014-01-01  11.269698  33.362217 2014-01-01
2014-02-01  19.667178  34.513765 2014-02-01
2014-03-01  12.604760  38.402551 2014-03-01
2014-04-01  18.972365  31.231021 2014-04-01
2014-05-01  13.767497  35.430262 2014-05-01

该系列有自己的索引与月份索引不匹配。 NumPy数组没有索引并使用您提供的索引。

相关问题