我遇到了 pandas.to_csv 在 datetime64 类型的列上删除值的问题。
In [24]: df
Out[24]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 28982 entries, 0 to 28981
Data columns (total 4 columns):
value 28982 non-null values
date1 28982 non-null values
date2 22772 non-null values
date3 28982 non-null values
dtypes: datetime64[ns](3), float64(1)
In [25]: df.tail()
Out[25]:
value date1 date2 date3
28977 25.44 2002-08-21 00:00:00 2013-05-03 00:00:00 2007-09-01 00:00:00
28978 25.86 2002-08-21 00:00:00 2013-05-03 00:00:00 2007-09-01 00:00:00
28979 26.08 2002-08-21 00:00:00 2013-05-03 00:00:00 2007-09-01 00:00:00
28980 25.84 2002-08-21 00:00:00 2013-05-03 00:00:00 2007-09-01 00:00:00
28981 25.35 2002-08-21 00:00:00 2013-05-03 00:00:00 2007-09-01 00:00:00
In [26]: df.to_csv('test.csv', index = False)
In [27]: df2 = pd.read_csv('test.csv', header = 0)
In [28]: df2
Out[28]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 28982 entries, 0 to 28981
Data columns (total 4 columns):
value 28982 non-null values
date1 28982 non-null values
date2 21070 non-null values
date3 17036 non-null values
dtypes: float64(1), object(3)
In [29]: df2.tail()
Out[29]:
value date1 date2 date3
28977 25.44 2002-08-21 00:00:00 NaN NaN
28978 25.86 2002-08-21 00:00:00 NaN NaN
28979 26.08 2002-08-21 00:00:00 NaN NaN
28980 25.84 2002-08-21 00:00:00 NaN NaN
28981 25.35 2002-08-21 00:00:00 NaN NaN
如图所示,我将 df 写入文件并立即将其读回 df2,csv 文件中的 date2 和 date3 列在底部有很多缺失值。这是一个错误吗?顺便说一句,我使用的是 Pandas 0.11。