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

问题描述 投票:0回答:2

我是一个新手熊猫用户,并为一些麻烦感到沮丧。

情况就是这样。这是最初的数据帧。

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 你能给我一些建议吗?

谢谢

python pandas
2个回答
0
投票

您可以尝试pd.to_datetime而不是timestamp并尝试


0
投票

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

Github Issues #16044

© www.soinside.com 2019 - 2024. All rights reserved.