У меня есть фрейм данных, который добавляется в цикл (если есть лучший способ итеративно добавлять строки в конец фрейма данных, тогда предложения приветствуются).Следующий фрагмент кода выдает ошибку:
import pandas as pd
import pytz
import datetime
x = 'astring'
t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
df = pd.DataFrame(columns=['a', 'b', 'c'])
df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)
TypeError Traceback (most recent call last)
<ipython-input-161-0df455a78607> in <module>()
2 t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
3 df = pd.DataFrame(columns=['a', 'b', 'c'])
----> 4 df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/frame.py in append(self, other, ignore_index, verify_integrity)
5192
5193 _shared_docs['pivot_table'] = """
-> 5194 Create a spreadsheet-style pivot table as a DataFrame. The levels in
5195 the pivot table will be stored in MultiIndex objects (hierarchical
5196 indexes) on the index and columns of the result DataFrame
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
211 a 1
212 >>> df6 = pd.DataFrame([2], index=['a'])
--> 213 >>> df6
214 0
215 a 2
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/reshape/concat.py in get_result(self)
406 mgrs_indexers = []
407 for obj in self.objs:
--> 408 mgr = obj._data
409 indexers = {}
410 for ax, new_labels in enumerate(self.new_axes):
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
5201 expanded label indexer
5202 """
-> 5203 mult = np.array(shape)[::-1].cumprod()[::-1]
5204 return _ensure_platform_int(
5205 np.sum(np.array(labels).T * np.append(mult, [1]), axis=1).T)
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in concatenate_join_units(join_units, concat_axis, copy)
5330
5331 # see if we are only masking values that if putted
-> 5332 # will work in the current dtype
5333 try:
5334 nn = n[m]
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in <listcomp>(.0)
5330
5331 # see if we are only masking values that if putted
-> 5332 # will work in the current dtype
5333 try:
5334 nn = n[m]
/usr/local/envs/py3env/lib/python3.5/site-packages/pandas/core/internals.py in get_reindexed_values(self, empty_dtype, upcasted_na)
5601 for ax, indexer in indexers.items():
5602 mgr_shape[ax] = len(indexer)
-> 5603 mgr_shape = tuple(mgr_shape)
5604
5605 if 0 in indexers:
TypeError: data type not understood
Тем не менее, следующий фрагмент отлично работает:
x = 'astring'
t = (datetime.datetime(2018, 5, 31, 13, 15, 17), datetime.datetime(2100, 5, 31))
df = pd.DataFrame(columns=['a', 'b', 'c'])
df = df.append({'a': x, 'b': t[0], 'c': t[1]}, ignore_index=True)
И, как ни странно, это тоже нормально:
t = (datetime.datetime(2018, 5, 31, 13, 15, 17, tzinfo=pytz.utc), datetime.datetime(2100, 5, 31, tzinfo=pytz.utc))
df = pd.DataFrame(columns=['b', 'c'])
df = df.append({'b': t[0], 'c': t[1]}, ignore_index=True)
Чего мне не хватает?Я просто добавляю здесь больше подробностей, потому что StackOverflow жалуется, что мне «нужно больше подробностей», чтобы отправить этот вопрос, потому что я думаю, что быть исключительно многословным - это хорошо.Кто знал?
pandas==0.23.0
pytz==2016.7