BUG: conversion of empty DataFrame to SparseDtype (#33113)
State: closed

- [x] closes #33113
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Labels:
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode
name: Sparse
description: Sparse Data Type

Comments:
rebase on master as have merged https://github.com/pandas-dev/pandas/pull/33110 which might fix this (if so, you can just push the test itself)
@jreback now this is synced with master, i removed my changes in `generic.py` and only left the test, which is still failing.
> @jreback now this is synced with master, i removed my changes in `generic.py` and only left the test, which is still failing.

then there maybe still an issue
@simonjayhawkins i've added the new test.
i've added a small fix by simply returning an empty dataframe when there's nothing to concatenate.
Thanks @tgy can you add a whatsnew
@simonjayhawkins done!
@simonjayhawkins @jreback it seems that using `tm.assert_frame_equal` is prevented by CI checks
 
```
##[error]pandas/tests/extension/base/dtype.py:119: 
tm.assert_frame_equal(empty_dataframe.astype(dtype), empty_dataframe)
```

maybe i should only do `empty_dataframe.astype(dtype)` instead, without checking the result.

what do you recommend?

there's also a typing validation error:

```
pandas/core/generic.py:5573: error: 
Incompatible return value type (got "DataFrame", expected "FrameOrSeries")
```

what should i do about that?
> it seems that using `tm.assert_frame_equal` is prevented by CI checks
> 
> ```
> ##[error]pandas/tests/extension/base/dtype.py:119: 
> tm.assert_frame_equal(empty_dataframe.astype(dtype), empty_dataframe)
> ```
> 
> maybe i should only do `empty_dataframe.astype(dtype)` instead, without checking the result.

That was my mistake asking for the test to be moved in https://github.com/pandas-dev/pandas/pull/33118#discussion_r399819166. We'll probably need to keep the test where is was and use parameterization instead.

We don't have a fixture specifically for extension dtypes in conftest.py. (It maybe elsewhere), so could manually copy the dtypes from each of the /tests/extensions/.. tests for now.
> there's also a typing validation error:
> 
> ```
> pandas/core/generic.py:5573: error: 
> Incompatible return value type (got "DataFrame", expected "FrameOrSeries")
> ```
> 
> what should i do about that?

The issue only affects DataFrame? but for mypy the return type needs to the same as the input type. maybe the original fix, see https://github.com/pandas-dev/pandas/pull/33118#discussion_r399801536, is more appropriate?
@jreback @simonjayhawkins I've updated the fix and moved the test. What do you guys think?
Hello @tgy! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:







There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-06-22 16:03:25 UTC
Thanks @tgy for being patient here and sorry for reviewing sooner

> it seems that using `tm.assert_frame_equal` is prevented by CI checks

ah my bad. should have been `self.assert_frame_equal`

i've moved the test (again!) to pandas\tests\extension\base\casting.py

can you add a release note to doc\source\whatsnew\v1.1.0.rst
@simonjayhawkins done! I was the one who postponed that issue :-) Thanks for reviewing so quickly on a Sunday
@simonjayhawkins let me know what you think :)
> @simonjayhawkins let me know what you think :)

see https://github.com/pandas-dev/pandas/pull/33118#discussion_r443619621
Thanks @tgy for adding the test. The test is not applicable to the extension array dtype tests. can you move it to pandas\tests\frame\methods\test_astype.py
The test will still fail with `name 'pd' is not defined` and thereafter with `AttributeError: 'TestAstype' object has no attribute 'assert_frame_equal'`

you can run it locally 

```
pytest pandas/tests/frame/methods/test_astype.py -k test_astype_empty_dtype_dict
```

can you also add `assert result is not df`
Thanks, should work now. Locally it did. 
thanks @tgy