str(data_frame) raises an exception when there is a column with both NaT value and Timestamp with timezone info · Issue #12211 · pandas-dev/pandas (original) (raw)
When you try to print a data frame that has a Timestamp column with both NaT values and Timestamp with timezone, AND the number or rows is more than the display setting - an exception is raised.
import pandas as pd
from datetime import datetime
from pytz import UTC
test=pd.DataFrame([{"dt": pd.Timestamp(datetime.now().replace(tzinfo=UTC)), "x": 1}] + [{"dt": pd.NaT, "x": 1}] * 5)
pd.options.display.max_rows = 5
str(test)
This is the traceback when running the code above with 0.17.1 (with 0.17.0, the traceback is different, with this error message: AttributeError: 'numpy.ndarray' object has no attribute 'tz_localize')
TypeError Traceback (most recent call last)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/IPython/core/formatters.py in call(self, obj)
339 method = _safe_get_formatter_method(obj, self.print_method)
340 if method is not None:
--> 341 return method()
342 return None
343 else:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/frame.py in repr_html(self)
573 return self.to_html(max_rows=max_rows, max_cols=max_cols,
574 show_dimensions=show_dimensions,
--> 575 notebook=True)
576 else:
577 return None
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/frame.py in to_html(self, buf, columns, col_space, colSpace, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, bold_rows, classes, escape, max_rows, max_cols, show_dimensions, notebook)
1529 max_rows=max_rows,
1530 max_cols=max_cols,
-> 1531 show_dimensions=show_dimensions)
1532 # TODO: a generic formatter wld b in DataFrameFormatter
1533 formatter.to_html(classes=classes, notebook=notebook)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/format.py in init(self, frame, buf, columns, col_space, header, index, na_rep, formatters, justify, float_format, sparsify, index_names, line_width, max_rows, max_cols, show_dimensions, **kwds)
378 self.columns = frame.columns
379
--> 380 self._chk_truncate()
381 self.adj = _get_adjustment()
382
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/format.py in _chk_truncate(self)
444 else:
445 row_num = max_rows_adj // 2
--> 446 frame = concat((frame.iloc[:row_num, :], frame.iloc[-row_num:, :]))
447 self.tr_row_num = row_num
448
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/tools/merge.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
811 verify_integrity=verify_integrity,
812 copy=copy)
--> 813 return op.get_result()
814
815
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/tools/merge.py in get_result(self)
993
994 new_data = concatenate_block_managers(
--> 995 mgrs_indexers, self.new_axes, concat_axis=self.axis, copy=self.copy)
996 if not self.copy:
997 new_data._consolidate_inplace()
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
4454 copy=copy),
4455 placement=placement)
-> 4456 for placement, join_units in concat_plan]
4457
4458 return BlockManager(blocks, axes)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in (.0)
4454 copy=copy),
4455 placement=placement)
-> 4456 for placement, join_units in concat_plan]
4457
4458 return BlockManager(blocks, axes)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in concatenate_join_units(join_units, concat_axis, copy)
4551 to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
4552 upcasted_na=upcasted_na)
-> 4553 for ju in join_units]
4554
4555 if len(to_concat) == 1:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in (.0)
4551 to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
4552 upcasted_na=upcasted_na)
-> 4553 for ju in join_units]
4554
4555 if len(to_concat) == 1:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in get_reindexed_values(self, empty_dtype, upcasted_na)
4799
4800 if self.is_null and not getattr(self.block,'is_categorical',None):
-> 4801 missing_arr = np.empty(self.shape, dtype=empty_dtype)
4802 if np.prod(self.shape):
4803 # NumPy 1.6 workaround: this statement gets strange if all
TypeError: data type not understood
TypeError Traceback (most recent call last)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/IPython/core/formatters.py in call(self, obj)
695 type_pprinters=self.type_printers,
696 deferred_pprinters=self.deferred_printers)
--> 697 printer.pretty(obj)
698 printer.flush()
699 return stream.getvalue()
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/IPython/lib/pretty.py in pretty(self, obj)
381 if callable(meth):
382 return meth(obj, self, cycle)
--> 383 return _default_pprint(obj, self, cycle)
384 finally:
385 self.end_group()
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
501 if _safe_getattr(klass, 'repr', None) not in baseclass_reprs:
502 # A user-provided repr. Find newlines and replace them with p.break()
--> 503 _repr_pprint(obj, p, cycle)
504 return
505 p.begin_group(1, '<')
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/IPython/lib/pretty.py in repr_pprint(obj, p, cycle)
683 """A pprint that just redirects to the normal repr function."""
684 # Find newlines and replace them with p.break()
--> 685 output = repr(obj)
686 for idx,output_line in enumerate(output.splitlines()):
687 if idx:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/base.py in repr(self)
61 Yields Bytestring in Py2, Unicode String in py3.
62 """
---> 63 return str(self)
64
65
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/base.py in str(self)
40
41 if compat.PY3:
---> 42 return self.unicode()
43 return self.bytes()
44
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/frame.py in unicode(self)
539 width = None
540 self.to_string(buf=buf, max_rows=max_rows, max_cols=max_cols,
--> 541 line_width=width, show_dimensions=show_dimensions)
542
543 return buf.getvalue()
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/frame.py in to_string(self, buf, columns, col_space, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, line_width, max_rows, max_cols, show_dimensions)
1478 max_rows=max_rows,
1479 max_cols=max_cols,
-> 1480 show_dimensions=show_dimensions)
1481 formatter.to_string()
1482
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/format.py in init(self, frame, buf, columns, col_space, header, index, na_rep, formatters, justify, float_format, sparsify, index_names, line_width, max_rows, max_cols, show_dimensions, **kwds)
378 self.columns = frame.columns
379
--> 380 self._chk_truncate()
381 self.adj = _get_adjustment()
382
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/format.py in _chk_truncate(self)
444 else:
445 row_num = max_rows_adj // 2
--> 446 frame = concat((frame.iloc[:row_num, :], frame.iloc[-row_num:, :]))
447 self.tr_row_num = row_num
448
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/tools/merge.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
811 verify_integrity=verify_integrity,
812 copy=copy)
--> 813 return op.get_result()
814
815
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/tools/merge.py in get_result(self)
993
994 new_data = concatenate_block_managers(
--> 995 mgrs_indexers, self.new_axes, concat_axis=self.axis, copy=self.copy)
996 if not self.copy:
997 new_data._consolidate_inplace()
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
4454 copy=copy),
4455 placement=placement)
-> 4456 for placement, join_units in concat_plan]
4457
4458 return BlockManager(blocks, axes)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in (.0)
4454 copy=copy),
4455 placement=placement)
-> 4456 for placement, join_units in concat_plan]
4457
4458 return BlockManager(blocks, axes)
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in concatenate_join_units(join_units, concat_axis, copy)
4551 to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
4552 upcasted_na=upcasted_na)
-> 4553 for ju in join_units]
4554
4555 if len(to_concat) == 1:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in (.0)
4551 to_concat = [ju.get_reindexed_values(empty_dtype=empty_dtype,
4552 upcasted_na=upcasted_na)
-> 4553 for ju in join_units]
4554
4555 if len(to_concat) == 1:
/Users/uzi/.virtualenvs/pandas/lib/python3.4/site-packages/pandas/core/internals.py in get_reindexed_values(self, empty_dtype, upcasted_na)
4799
4800 if self.is_null and not getattr(self.block,'is_categorical',None):
-> 4801 missing_arr = np.empty(self.shape, dtype=empty_dtype)
4802 if np.prod(self.shape):
4803 # NumPy 1.6 workaround: this statement gets strange if all
TypeError: data type not understood