Skip to content

Commit 27b42a5

Browse files
committed
BUG-25061 fix printing indices with NaNs
1 parent 638ddeb commit 27b42a5

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v0.24.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Bug Fixes
5252
**I/O**
5353

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

5858
**Categorical**

pandas/io/formats/format.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,11 @@ def get_result_as_array(self):
10601060
def format_values_with(float_format):
10611061
formatter = self._value_formatter(float_format, threshold)
10621062

1063+
# default formatter leaves a space to the left when formatting
1064+
# floats, must be consistent for left-justifying NaNs (GH #25061)
1065+
if self.justify == 'left':
1066+
self.na_rep = ' ' + self.na_rep
1067+
10631068
# separate the wheat from the chaff
10641069
values = self.values
10651070
mask = isna(values)

pandas/tests/series/test_repr.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ def test_latex_repr(self):
198198

199199
assert s._repr_latex_() is None
200200

201+
def test_categorical_index_repr_in_frame_with_nan(self):
202+
i = Index([1, np.nan])
203+
s = Series([1, 2], index=i)
204+
exp = """1.0 1\nNaN 2\ndtype: int64"""
205+
206+
assert repr(s) == exp
207+
201208

202209
class TestCategoricalRepr(object):
203210

0 commit comments

Comments
 (0)