Skip to content

Commit e37ff6e

Browse files
authored
BUG: conversion of empty DataFrame to SparseDtype (#33113) (#33118)
1 parent 91802a9 commit e37ff6e

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ Sparse
11011101
- Bug in :meth:`Series.sum` with ``SparseArray`` raises ``TypeError`` (:issue:`25777`)
11021102
- Bug where :class:`DataFrame` containing :class:`SparseArray` filled with ``NaN`` when indexed by a list-like (:issue:`27781`, :issue:`29563`)
11031103
- The repr of :class:`SparseDtype` now includes the repr of its ``fill_value`` attribute. Previously it used ``fill_value``'s string representation (:issue:`34352`)
1104+
- Bug where empty :class:`DataFrame` could not be cast to :class:`SparseDtype` (:issue:`33113`)
11041105

11051106
ExtensionArray
11061107
^^^^^^^^^^^^^^

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -5537,6 +5537,10 @@ def astype(
55375537
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
55385538
return self._constructor(new_data).__finalize__(self, method="astype")
55395539

5540+
# GH 33113: handle empty frame or series
5541+
if not results:
5542+
return self.copy()
5543+
55405544
# GH 19920: retain column metadata after concat
55415545
result = pd.concat(results, axis=1, copy=False)
55425546
result.columns = self.columns

pandas/tests/extension/base/casting.py

+6
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ def test_to_numpy(self, data):
5050

5151
result = pd.Series(data).to_numpy()
5252
self.assert_equal(result, expected)
53+
54+
def test_astype_empty_dataframe(self, dtype):
55+
# https://github.com/pandas-dev/pandas/issues/33113
56+
df = pd.DataFrame()
57+
result = df.astype(dtype)
58+
self.assert_frame_equal(result, df)

pandas/tests/frame/methods/test_astype.py

+8
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,11 @@ def test_astype_dt64tz_to_str(self, timezone_frame):
557557
assert (
558558
"2 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-03 00:00:00+01:00"
559559
) in result
560+
561+
def test_astype_empty_dtype_dict(self):
562+
# issue mentioned further down in the following issue's thread
563+
# https://github.com/pandas-dev/pandas/issues/33113
564+
df = DataFrame()
565+
result = df.astype(dict())
566+
tm.assert_frame_equal(result, df)
567+
assert result is not df

0 commit comments

Comments
 (0)