在某些情况下,无法将系列追加到数据帧

时间:2017-12-09 09:11:06

标签: python pandas

我是一个新手熊猫用户,并为一些麻烦而烦恼。

在这种情况下。 这是最初的数据框架。

In [9]: df
Out[9]: 
  importance interval last_read                last_update    name trigger
0          2      NaN       NaN  2017-12-09 00:00:00+09:00  foobar     NaN

这是要追加的记录。

In [10]: record = df.iloc[0].copy()

追加此记录时没有错误。

In [11]: df.append(record)
Out[11]: 
  importance interval last_read                last_update    name trigger
0          2      NaN       NaN  2017-12-09 00:00:00+09:00  foobar     NaN
0          2      NaN       NaN  2017-12-09 00:00:00+09:00  foobar     NaN

将记录的NaN值更改为int并追加它,并且不会发生错误。

In [12]: record['interval'] = 1

In [13]: df.append(record)
Out[13]: 
  importance interval last_read                last_update    name trigger
0          2      NaN       NaN  2017-12-09 00:00:00+09:00  foobar     NaN
0          2        1       NaN  2017-12-09 00:00:00+09:00  foobar     NaN

然后将记录的另一个NaN值更改为pd.Timestamp并追加。 这发生了错误。

In [10]: record
Out[10]: 
importance                             2
interval                             NaN
last_read                            NaN
last_update    2017-12-09 00:00:00+09:00
name                              foobar
trigger                              NaN
Name: 0, dtype: object

In [11]: record['trigger'] = record['last_update']

In [12]: record
Out[12]: 
importance                             2
interval                             NaN
last_read                            NaN
last_update    2017-12-09 00:00:00+09:00
name                              foobar
trigger        2017-12-09 00:00:00+09:00
Name: 0, dtype: object

In [13]: df.append(record)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-7c027f1cbb54> in <module>()
----> 1 df.append(record)

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in append(self, other, ignore_index, verify_integrity)
   4545             to_concat = [self, other]
   4546         return concat(to_concat, ignore_index=ignore_index,
-> 4547                       verify_integrity=verify_integrity)
   4548 
   4549     def join(self, other, on=None, how='left', lsuffix='', rsuffix='',

~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
    205                        verify_integrity=verify_integrity,
    206                        copy=copy)
--> 207     return op.get_result()
    208 
    209 

~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py in get_result(self)
    405             new_data = concatenate_block_managers(
    406                 mgrs_indexers, self.new_axes, concat_axis=self.axis,
--> 407                 copy=self.copy)
    408             if not self.copy:
    409                 new_data._consolidate_inplace()

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
   4830     blocks = [make_block(
   4831         concatenate_join_units(join_units, concat_axis, copy=copy),
-> 4832         placement=placement) for placement, join_units in concat_plan]
   4833 
   4834     return BlockManager(blocks, axes)

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in <listcomp>(.0)
   4830     blocks = [make_block(
   4831         concatenate_join_units(join_units, concat_axis, copy=copy),
-> 4832         placement=placement) for placement, join_units in concat_plan]
   4833 
   4834     return BlockManager(blocks, axes)

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in concatenate_join_units(join_units, concat_axis, copy)
   4937     to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
   4938                                          upcasted_na=upcasted_na)
-> 4939                  for ju in join_units]
   4940 
   4941     if len(to_concat) == 1:

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in <listcomp>(.0)
   4937     to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
   4938                                          upcasted_na=upcasted_na)
-> 4939                  for ju in join_units]
   4940 
   4941     if len(to_concat) == 1:

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in get_reindexed_values(self, empty_dtype, upcasted_na)
   5210                     pass
   5211                 else:
-> 5212                     missing_arr = np.empty(self.shape, dtype=empty_dtype)
   5213                     missing_arr.fill(fill_value)
   5214                     return missing_arr

TypeError: data type not understood

我的熊猫版本是0.20.3
你能给我一些建议吗?

谢谢

2 个答案:

答案 0 :(得分:0)

您可以尝试pd.to_datetime而不是时间戳,然后尝试

答案 1 :(得分:0)

这是一个错误。 请参阅以下链接。

Github Issues #16044