为什么会这样?
import pandas as pd
import numpy as np
grid = pd.DataFrame(np.random.randn(10,3), columns=["X", "Y", "Z"])
year_velocities = pd.DataFrame()
hours = pd.date_range(start='1/1/2010', periods=10, freq='H')
temp_df = pd.DataFrame()
for h in hours:
print h
temp_df = grid.copy()
temp_df["time"] = h.strftime("%Y-%m-%d %H:%M:%S")
year_velocities = year_velocities.append(temp_df, ignore_index=True)
如果我更换
temp_df["time"] = h.strftime("%Y-%m-%d %H:%M:%S")
带
temp_df["time"] = h
它失败了?
我最终得到一个空的temp_df。
似乎我无法将TimeStamp分配给列?
更新:代码适用于这两种情况。当我尝试使用Anaconda Spyder Variable Explorer可视化temp_df和year_velocities的值时,会出现此问题。
>>> Traceback (most recent call last):
File "C:\Users\aerg01341\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\dicteditor.py", line 411, in createEditor
if not editor.setup_and_check(value):
File "C:\Users\aerg01341\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\dataframeeditor.py", line 409, in setup_and_check
self.dataModel = DataFrameModel(data, parent=self)
File "C:\Users\aerg01341\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\dataframeeditor.py", line 75, in __init__
self.max_min_col_update()
File "C:\Users\aerg01341\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\dataframeeditor.py", line 90, in max_min_col_update
df_abs = self.df[self.complex_intran].abs()
File "C:\Users\aerg01341\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\generic.py", line 3662, in abs
return np.abs(self)
TypeError: bad operand type for abs(): 'NaTType'