Skip to content

str(data_frame) raises an exception when there is a column with both NaT value and Timestamp with timezone info #12211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
uzihs opened this issue Feb 2, 2016 · 9 comments
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Timezones Timezone data dtype
Milestone

Comments

@uzihs
Copy link

uzihs commented Feb 2, 2016

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

@jreback
Copy link
Contributor

jreback commented Feb 2, 2016

Hmm I just fixed this for the Series constructor, but the DataFrame construtor does a different introspection.

In [14]: Series([pd.Timestamp(datetime.now().replace(tzinfo=UTC))]+[pd.NaT]*5)         
Out[14]: 
0   2016-02-02 16:53:41.321229+00:00
1                                NaT
                  ...               
4                                NaT
5                                NaT
dtype: datetime64[ns, UTC]

@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Timezones Timezone data dtype labels Feb 2, 2016
@jreback jreback added this to the 0.18.0 milestone Feb 2, 2016
@jreback jreback modified the milestones: Next Major Release, 0.18.0 Feb 9, 2016
@uzihs
Copy link
Author

uzihs commented Mar 23, 2016

I see that it's assigned to 0.18.0. But it is not yet resolved in the version that was released last week. Right?

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

no, its assigned to next major release, which is kind of a catch-all to say its an open issue. so this is not resolved.

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

Actually this does look fixed though. #11693, #11755, #12217 were all fixed in 0.18.0. could have been caught there.

In [9]: test
Out[9]: 
                                dt  x
0 2016-03-23 11:11:48.441987+00:00  1
1                              NaT  1
2                              NaT  1
3                              NaT  1
4                              NaT  1
5                              NaT  1

Want to do some confirming tests?

@jreback jreback modified the milestones: 0.18.1, Next Major Release Mar 23, 2016
@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

dc6c678 or referenced issues might be the fixes

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

actually forgot the part with the max_rows, nvm. This is still an issue.

@uzihs
Copy link
Author

uzihs commented Mar 23, 2016

Right. I tested the example I posted above, with 0.18.0. Same behaviour...

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

the root cause is #11594 (and I point to a fix there). Its pretty straightforward to fix actually.

@sinhrks
Copy link
Member

sinhrks commented Apr 30, 2016

This is already fixed (maybe by #12195). Adding test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants