ENH: Support fspath protocol
State: closed

Supports the [fspath protocol](https://www.python.org/dev/peps/pep-0519/) in most readers and writers.

closes https://github.com/pandas-dev/pandas/issues/13823
xref https://github.com/pandas-dev/pandas/pull/14123

Labels:
name: IO Data
description: IO issues that don't fit into a more specific label
name: Compat
description: pandas objects compatability with Numpy or Python functions

Comments:
Wait till 0.21 on this? Technically this _could_ be an API change for if people using `ExcelFile` directly, as I stringify `self.io`. I can probably work around this.

This will cause one error in the pytables tests, since *closed* HDFStore objects don't allow object access, so I can't check `hasattr(obj, '__fspath__')`. I think I'll change `HDFStore.__getattr__`, but have to run for now.

Also, any feedback on the testing method? Typically we have the tests split by the kind of reader / writer. But for things like these protocols, it's nice to test the interface against every reader / writer that should support it, so I put in in `common.py`.
https://github.com/pandas-dev/pandas/pull/16301/commits/8c10784f816ef9d7bb224df335229bdbf02a368f has the API changes required on HDFStore. Relatively minor, so I'm ok with just changing it in 0.21, but we can do a deprecation if you want, or I can just code around it in `_stringify_path` and we never change it.
> pandas-dev/pandas@8c10784 has the API changes required on HDFStore. Relatively minor, so I'm ok with just changing it in 0.21, but we can do a deprecation if you want, or I can just code around it in _stringify_path and we never change it.

ok to just change this
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=h1) Report
> Merging [#16301](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/09d1c97f5030dbb7779d30ce0c9622321fd43304?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `91.89%`.

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

```diff
@@            Coverage Diff             @@
##           master   #16301      +/-   ##
==========================================
+ Coverage   90.39%   90.39%   +<.01%     
==========================================
  Files         161      161              
  Lines       50863    50877      +14     
==========================================
+ Hits        45977    45990      +13     
- Misses       4886     4887       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.22% <91.89%> (+0.06%)` | :arrow_up: |
| #single | `40.33% <40.54%> (-0.11%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3B5dGFibGVzLnB5) | `93.06% <ø> (-0.01%)` | :arrow_down: |
| [pandas/io/packers.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhY2tlcnMucHk=) | `88.23% <100%> (+0.03%)` | :arrow_up: |
| [pandas/io/feather\_format.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2ZlYXRoZXJfZm9ybWF0LnB5) | `87.87% <100%> (+1.21%)` | :arrow_up: |
| [pandas/io/formats/excel.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZXhjZWwucHk=) | `74.33% <100%> (+1.17%)` | :arrow_up: |
| [pandas/io/pickle.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3BpY2tsZS5weQ==) | `80.43% <100%> (+0.88%)` | :arrow_up: |
| [pandas/io/stata.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3N0YXRhLnB5) | `93.47% <100%> (ø)` | :arrow_up: |
| [pandas/io/json/json.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2pzb24vanNvbi5weQ==) | `90.36% <100%> (+0.02%)` | :arrow_up: |
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `95.66% <100%> (ø)` | :arrow_up: |
| [pandas/io/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2NvbW1vbi5weQ==) | `69.78% <100%> (-0.3%)` | :arrow_down: |
| [pandas/io/sas/sasreader.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXNyZWFkZXIucHk=) | `86.2% <100%> (+1.02%)` | :arrow_up: |
| ... and [6 more](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16301?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/16301?src=pr&el=footer). Last update [09d1c97...03d29ba](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@539de79`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `93.87%`.

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

```diff
@@            Coverage Diff            @@
##             master   #16301   +/-   ##
=========================================
  Coverage          ?    90.4%           
=========================================
  Files             ?      161           
  Lines             ?    50987           
  Branches          ?        0           
=========================================
  Hits              ?    46096           
  Misses            ?     4891           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.24% <83.67%> (?)` | |
| #single | `40.19% <55.1%> (?)` | |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `95.66% <100%> (ø)` | |
| [pandas/io/json/json.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2pzb24vanNvbi5weQ==) | `90.36% <100%> (ø)` | |
| [pandas/io/stata.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3N0YXRhLnB5) | `93.47% <100%> (ø)` | |
| [pandas/io/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2NvbW1vbi5weQ==) | `69.91% <100%> (ø)` | |
| [pandas/io/pickle.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3BpY2tsZS5weQ==) | `81.25% <100%> (ø)` | |
| [pandas/io/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3B5dGFibGVzLnB5) | `93.06% <100%> (ø)` | |
| [pandas/io/feather\_format.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2ZlYXRoZXJfZm9ybWF0LnB5) | `87.87% <100%> (ø)` | |
| [pandas/io/formats/excel.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZXhjZWwucHk=) | `74.24% <100%> (ø)` | |
| [pandas/io/packers.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhY2tlcnMucHk=) | `88.23% <100%> (ø)` | |
| [pandas/io/sas/sasreader.py](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXNyZWFkZXIucHk=) | `86.2% <100%> (ø)` | |
| ... and [1 more](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16301?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/16301?src=pr&el=footer). Last update [539de79...ac37895](https://codecov.io/gh/pandas-dev/pandas/pull/16301?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Rebased on top of #16292 

I also added a third commit adding `__fspath__` to our own path-like objects: HDFStore and ExcelFile. Do we have any others?
We also have `ExcelWriter`
@jreback do we have any official policy on whether or not to include package data in the distribution? This test uses a pickle that's not included, so it was failing on the build test. https://github.com/pandas-dev/pandas/pull/16301/commits/e3b3fcb9c672bab35d6d2773e6ef8de8cbd87e4b works around it.

Right now our wheel is ~38M unzipped, and about 15M of that are test files.
> @jreback do we have any official policy on whether or not to include package data in the distribution? This test uses a pickle that's not included, so it was failing on the build test. e3b3fcb works around it.

we DO include things, but should only be smallish test files. Don't work around things like that.
I think your file is not included, because its not in the filter for package_data (for ``.mp``)
> I think your file is not included, because its not in the filter for package_data (for .mp)

Completely forgot that I added the messagepack file :/ Fixed now.

All green, other than that matplotlib test, which I'm going to debug now.
@TomAugspurger lgtm. plotting occasional failures are unrelated and I think you have an issue for that anyhow.
Thanks. I'm (trying to) debug the plotting stuff on my Travis branch, where it's currently not failing :/