Repository: pandas-dev/pandas

Issue:
Formatting of CategoricalIndex misaligned if NaNs are present
#### Code Sample, a copy-pastable example if possible

```python
>>> import pandas as pd
>>> pd.DataFrame([[1],[2]], index=pd.CategoricalIndex([1, pd.np.nan]))
      0
 1.0  1
NaN   2
```
#### Problem description

`CategoricalIndex` of a `DataFrame` is misaligned when printing if it contains `NaN` values.

#### Expected Output

```python
>>> import pandas as pd
>>> pd.DataFrame([[1],[2]], index=pd.CategoricalIndex([1, pd.np.nan]))
      0
1.0   1
NaN   2
```

#### Output of ``pd.show_versions()``

<details>

No module named 'dask'

INSTALLED VERSIONS
------------------
commit: da5f5eb6c11140a2bb9f84049d30ba7d89bdbfd0
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 18.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8

pandas: 0.24.0.dev0+1684.gda5f5eb6c
pytest: 4.0.0
pip: 10.0.1
setuptools: 40.6.2
Cython: 0.29
numpy: 1.14.2
scipy: 1.1.0
pyarrow: 0.11.1
xarray: 0.11.0
IPython: 7.1.1
sphinx: 1.8.2
patsy: 0.5.0
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.1
openpyxl: 2.5.9
xlrd: 1.1.0
xlwt: 1.2.0
xlsxwriter: 1.1.2
lxml.etree: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.14
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: 0.1.6
fastparquet: 0.2.1
pandas_gbq: None
pandas_datareader: None
gcsfs: 0.1.2

</details>



Discussion:

Resolving patch:
diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py
--- a/pandas/io/formats/format.py
+++ b/pandas/io/formats/format.py
@@ -1060,19 +1060,26 @@ def get_result_as_array(self):
         def format_values_with(float_format):
             formatter = self._value_formatter(float_format, threshold)
 
+            # default formatter leaves a space to the left when formatting
+            # floats, must be consistent for left-justifying NaNs (GH #25061)
+            if self.justify == 'left':
+                na_rep = ' ' + self.na_rep
+            else:
+                na_rep = self.na_rep
+
             # separate the wheat from the chaff
             values = self.values
             mask = isna(values)
             if hasattr(values, 'to_dense'):  # sparse numpy ndarray
                 values = values.to_dense()
             values = np.array(values, dtype='object')
-            values[mask] = self.na_rep
+            values[mask] = na_rep
             imask = (~mask).ravel()
             values.flat[imask] = np.array([formatter(val)
                                            for val in values.ravel()[imask]])
 
             if self.fixed_width:
-                return _trim_zeros(values, self.na_rep)
+                return _trim_zeros(values, na_rep)
 
             return values
 


Test patch:
diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py
--- a/pandas/tests/series/test_repr.py
+++ b/pandas/tests/series/test_repr.py
@@ -198,6 +198,14 @@ def test_latex_repr(self):
 
         assert s._repr_latex_() is None
 
+    def test_index_repr_in_frame_with_nan(self):
+        # see gh-25061
+        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):