Skip to content

BUG-25061 fix printing indices with NaNs #25202

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

Merged
merged 5 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Bug Fixes
**I/O**

- Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`)
-
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)
-

**Categorical**
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,11 @@ def get_result_as_array(self):
def format_values_with(float_format):
formatter = self._value_formatter(float_format, threshold)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do
na_rep = self.na_rep here, then update (and use that value); so the value attached to self is not changed.

# default formatter leaves a space to the left when formatting
# floats, must be consistent for left-justifying NaNs (GH #25061)
if self.justify == 'left':
self.na_rep = ' ' + self.na_rep

# separate the wheat from the chaff
values = self.values
mask = isna(values)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ def test_latex_repr(self):

assert s._repr_latex_() is None

def test_categorical_index_repr_in_frame_with_nan(self):
i = Index([1, np.nan])
s = Series([1, 2], index=i)
exp = """1.0 1\nNaN 2\ndtype: int64"""

assert repr(s) == exp


class TestCategoricalRepr(object):

Expand Down