BUG: Slicing subclasses of SparseDataFrames.
State: closed

- [X] 1 test added & passed
- [X] passes `git diff upstream/master | flake8 --diff`

This changes `SparseDataFrame` to use proper subclassing functionality so slicing of subclasses of `SparseDataFrame` works. Example of a failure that this PR fixes:

``` python
import pandas as pd

class DenseSubclassDF(pd.DataFrame):
    _constructor = property(lambda self: DenseSubclassDF)
    _constructor_sliced = property(lambda self: DenseSubclassS)

class DenseSubclassS(pd.Series):
    _constructor = property(lambda self: DenseSubclassS)
    _constructor_expanddim = property(lambda self: DenseSubclassDF)

class SparseSubclassDF(pd.SparseDataFrame):
    _constructor = property(lambda self: SparseSubclassDF)
    _constructor_sliced = property(lambda self: SparseSubclassS)

class SparseSubclassS(pd.SparseSeries):
    _constructor = property(lambda self: SparseSubclassS)
    _constructor_expanddim = property(lambda self: SparseSubclassDF)

ddf = DenseSubclassDF([[1,2,3], [4,5,6], [7,8,9]])
sdf = SparseSubclassDF([[1,2,3], [4,5,6], [7,8,9]])

print(type(ddf.iloc[0]))  # <class '__main__.DenseSubclassS'>
print(type(ddf.iloc[:2]))  # <class '__main__.DenseSubclassDF'>
print(type(ddf[:2]))  # <class '__main__.DenseSubclassDF'>

# sparse doesn't preserve types
print(type(sdf.iloc[0]))  # <class '__main__.SparseSubclassS'>
print(type(sdf.iloc[:2]))  # <class 'pandas.sparse.frame.SparseDataFrame'>
print(type(sdf[:2]))  # <class 'pandas.sparse.frame.SparseDataFrame'>
```


Labels:
name: Sparse
description: Sparse Data Type
name: Compat
description: pandas objects compatability with Numpy or Python functions

Comments:
is there a specific issue for this?

@sinhrks pls have a look

## [Current coverage](https://codecov.io/gh/pydata/pandas/pull/13787?src=pr) is 85.25% (diff: 100%)

> Merging [#13787](https://codecov.io/gh/pydata/pandas/pull/13787?src=pr) into [master](https://codecov.io/gh/pydata/pandas/branch/master?src=pr) will increase coverage by **<.01%**

``` diff
@@             master     #13787   diff @@
==========================================
  Files           140        140          
  Lines         50455      50471    +16   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          43014      43031    +17   
+ Misses         7441       7440     -1   
  Partials          0          0          
```

> Powered by [Codecov](https://codecov.io?src=pr). Last update [59f2557...a7fb24f](https://codecov.io/gh/pydata/pandas/compare/59f25575f42e19f066dc3cd2245983071e2d7813...a7fb24f3a5636dcd24d05ce21fcf3c37c4ea33d2?src=pr)

Thank you for your comments, I made changes according to them:
- `.reindex` didn't need to be adapted, the problem was `_reindex_columns` which explicitly instantiated a `SparseDataFrame`. I changed all such calls to use `_constructor`.
- The thing to check in `testing.py` is that I added `obj` as a kwarg to panel checks to conform to what the series and frame ones look like. Previously, `obj` was referenced in them but the one that was used was a leftover from the for loop around line 2600. 
- There is no issue that handles this problem, the closest one is the sparse master issue #10627. Reference that one or create a new one?

@sstanovnik thx for update! i linked the PR from #10627, so new issue is not needed unless there is something which is not fixid in this PR.

Should I also squash the commits?

not mandatory on your side. will be done during merge.

Typo fixed, rebased. 

Two old pickling tests failed: see [this build](https://travis-ci.org/pydata/pandas/jobs/147445146#L1263). I didn't see a nice way out, so I made an exception for the deprecated SparseTimeSeries. This may not be what you want and needs review. 

@sstanovnik yeah `SparseTimeSeries` is deprecated, you can switch validation option depending on the version, like:
- https://github.com/pydata/pandas/blob/master/pandas/io/tests/test_pickle.py#L120

I added additional tests to check other subclassing changes I made. 

lgtm; though I think the read_pickle assertion can be done in a clearer way (use sep function). @sinhrks ?

@sstanovnik looks good. @sinhrks anything further?

None. thanks for all the effort, @sstanovnik !

@sstanovnik lgtm. pls add a whatsnew note (put in API changes). ping when green.

ping

thanks @sstanovnik