CLN: move common printing utilties to pandas.io.formats.printing
State: closed


Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string
name: Clean
name: ExtensionArray
description: Extending pandas with custom dtypes or arrays.

Comments:
these are *really* useful for ExtensionArrays to avoid re-inventing the wheel on repr. IIRC @TomAugspurger this was a sore point in creating IntervalArray.
e.g. get this (truncation, justification and line wrapping) on IntegerArray with no work

```
In [1]: pd.Series([1,2,np.nan]*20,dtype='Int8').values
Out[1]: 
IntegerArray([1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan,
              1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan,
              1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan, 1, 2, nan,
              1, 2, nan, 1, 2, nan],
             dtype='Int64')

In [2]: pd.Series([1,2,np.nan]*50,dtype='Int8').values
Out[2]: 
IntegerArray([1, 2, nan, 1, 2, nan, 1, 2, nan, 1,
              ...
              nan, 1, 2, nan, 1, 2, nan, 1, 2, nan],
             dtype='Int64', length=150)
```

with this code (can consider adding this as a function somewhere as well)
```
    def __repr__(self):
        """
        Return a string representation for this object.

        Invoked by unicode(df) in py2 only. Yields a Unicode String in both
        py2/py3.
        """
        klass = self.__class__.__name__
        data = format_object_summary(self, default_pprint, False, name=klass)
        attrs = format_object_attrs(self)
        space = " "

        prepr = (u(",%s") %
                 space).join(u("%s=%s") % (k, v) for k, v in attrs)

        res = u("%s(%s%s)") % (klass, data, prepr)

        return res
```
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=h1) Report
> Merging [#21234](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/36c1f6bfeb0d5915e1cd0bca1c91ee5672d945e7?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `97.53%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/21234/graphs/tree.svg?token=eZ4WkYLtcO&src=pr&height=150&width=650)](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #21234      +/-   ##
==========================================
- Coverage   91.84%   91.84%   -0.01%     
==========================================
  Files         153      153              
  Lines       49506    49512       +6     
==========================================
+ Hits        45467    45472       +5     
- Misses       4039     4040       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.23% <97.53%> (-0.01%)` | :arrow_down: |
| #single | `41.88% <79.01%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/21234/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9iYXNlLnB5) | `96.61% <100%> (-0.08%)` | :arrow_down: |
| [pandas/io/formats/printing.py](https://codecov.io/gh/pandas-dev/pandas/pull/21234/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvcHJpbnRpbmcucHk=) | `92.55% <97.33%> (+3.17%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=footer). Last update [36c1f6b...7e5dfcc](https://codecov.io/gh/pandas-dev/pandas/pull/21234?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).