如果现有DataFrame的最后一列属于datetime类型,我无法将新列连接到现有的Pandas DataFrame。这是一个最小的例子:
import pandas as pd
import numpy as np
dates = [pd.Timestamp('2012-05-01'), pd.Timestamp('2012-05-02'),
pd.Timestamp('2012-05-03')]
ed = pd.DataFrame( dates, index = range(3), columns=['Time'])
ed['Time'] = ed['Time'].dt.tz_localize('UTC').dt.tz_convert('US/Central')
ed = pd.concat([ed, pd.DataFrame(columns = [ 'Column1', 'Column2']
)], sort = False)
Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2878, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-c82182e48b5f>", line 8, in <module>
ed = pd.concat([ed, pd.DataFrame(columns = [ 'Column1', 'Column2'] )], sort = False)
File "C:\Anaconda2\lib\site-packages\pandas\core\reshape\concat.py", line 226, in concat
return op.get_result()
File "C:\Anaconda2\lib\site-packages\pandas\core\reshape\concat.py", line 423, in get_result
copy=self.copy)
File "C:\Anaconda2\lib\site-packages\pandas\core\internals.py", line 5421, in concatenate_block_managers
concatenate_join_units(join_units, concat_axis, copy=copy),
File "C:\Anaconda2\lib\site-packages\pandas\core\internals.py", line 5565, in concatenate_join_units
for ju in join_units]
File "C:\Anaconda2\lib\site-packages\pandas\core\internals.py", line 5851, in get_reindexed_values
if not self.block._can_consolidate:
AttributeError: 'NoneType' object has no attribute '_can_consolidate'
答案 0 :(得分:0)
在将ed['Time']
与新df联系后更改ed
列会怎样?
dates = [pd.Timestamp('2012-05-01'), pd.Timestamp('2012-05-02'), pd.Timestamp('2012-05-03')]
ed = pd.DataFrame( dates, index = range(3), columns=['Time'])
ed['Time'] = ed['Time'].dt.tz_localize('UTC').dt.tz_convert('US/Central')
ed = pd.concat([ed, pd.DataFrame(columns = [ 'Column1', 'Column2'])], sort = False, axis=1)