Ich bin ein Neuling Pandas Benutzer und für einige Schwierigkeiten verärgern.Kann in einigen Fällen keine Serie an den Datenrahmen anhängen
Hier ist der Fall. Dies ist der anfängliche Datenrahmen.
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
Und das ist der Datensatz zum Anhängen.
In [10]: record = df.iloc[0].copy()
Kein Fehler beim Anhängen dieses Datensatzes.
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
Ändern Sie einen NaN-Wert des Datensatzes in int und hängen Sie ihn an, und es tritt kein Fehler auf.
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
Dann ändern Sie einen anderen NaN-Wert des Datensatzes in pd.Timestamp und append. Dies tritt ein Fehler auf.
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
Meine Pandas Version 0.20.3
Könnten Sie mir einen Rat geben?
Danke
Vielen Dank für Ihre Replay und ich bin für mein schlechtes Codebeispiel sorry. Wie ich verstanden habe, rietst du mir, die Funktion pd.to_datetime zu verwenden, um einen Zeitstempel erfolgreich zu erstellen. In diesem Fall wurde jedoch der Zeitstempel erfolgreich generiert. (nur vom Timestamp-Wert der anderen Zelle kopiert) –