Skip to content

BUG: pandas.DataFrame().stack() raise an error, while expected is empty #36185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
165fd72
BUG: GH36113
steveya Sep 7, 2020
e0c1a8d
modify tests to avoid deprrecated errors
steveya Sep 7, 2020
f765acf
PEP 8 compliant
steveya Sep 7, 2020
109d312
remove trailing white space
steveya Sep 7, 2020
519a140
black format checked
steveya Sep 7, 2020
9d20ff5
DataFrame().stack should return an empty Series with dtype np.float64…
steveya Sep 9, 2020
d460db6
PEP8 again.
steveya Sep 9, 2020
bae2bd8
remove trailing space...\
steveya Sep 9, 2020
047ae40
add a comma to pass black lint
steveya Sep 9, 2020
c0fffe8
simply fixes and parameterize tests
steveya Sep 9, 2020
6b2b9bd
add error messages when unstack frame and series with single level index
steveya Sep 12, 2020
efc0603
apply ValueError location
steveya Sep 12, 2020
dac5f32
change the place where error is raised
steveya Sep 13, 2020
6524a6c
add a test for unstack series with one level of index. elaborate chan…
steveya Sep 16, 2020
6c71101
adding type information to exception message.
steveya Sep 22, 2020
e57aa51
Merge branch 'master' into GH36113
jreback Oct 10, 2020
f2f29bc
fix black format problem
steveya Oct 24, 2020
07d9ad5
resolve doc/source/whatsnew/v1.2.0.rst conflicts
steveya Nov 5, 2020
148b77d
fix unittest assert error message
steveya Nov 5, 2020
99f8280
change dtype of empty series and dataframe in test
steveya Nov 5, 2020
c4e244a
formatting
steveya Nov 5, 2020
668189f
change intp to int64 in testing of stack unstack empty frame
steveya Nov 20, 2020
20858db
Merge remote-tracking branch 'upstream/master' into GH36113
steveya Nov 20, 2020
4f95523
ensure indexer is of type int64
steveya Nov 22, 2020
7ab1155
Merge remote-tracking branch 'upstream/master' into GH36113
steveya Nov 25, 2020
475f158
remove xfail
steveya Nov 26, 2020
bdf49d3
remove unsed import
steveya Nov 26, 2020
f96453e
Merge remote-tracking branch 'upstream/master' into GH36113
steveya Nov 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Reshaping
- Bug in :meth:`DataFrame.pivot_table` with ``aggfunc='count'`` or ``aggfunc='sum'`` returning ``NaN`` for missing categories when pivoted on a ``Categorical``. Now returning ``0`` (:issue:`31422`)
- Bug in :func:`union_indexes` where input index names are not preserved in some cases. Affects :func:`concat` and :class:`DataFrame` constructor (:issue:`13475`)
- Bug in func :meth:`crosstab` when using multiple columns with ``margins=True`` and ``normalize=True`` (:issue:`35144`)
-
- Bug in :meth:`DataFrame.stack` for empty DataFrame (:issue:`36113`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you elaborate a bit on what is changing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have elaborate this a bit in the latest commit


Sparse
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def factorize(index):
# For homogeneous EAs, frame._values will coerce to object. So
# we concatenate instead.
dtypes = list(frame.dtypes._values)
dtype = dtypes[0]
dtype = dtypes[0] if len(dtypes) > 0 else object
Copy link
Member

@ivanovmg ivanovmg Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you return the Series right away if the dataframe is empty?

        if not frame.empty:
            dtypes = list(frame.dtypes._values)
            dtype = dtypes[0]
        else:
            return Series([], dtype=object)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you would still need to solve unstacking from an empty series.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likely you can just add a not frame.empty and frame._is_homogenerous_type on L516 might work


if is_extension_array_dtype(dtype):
arr = dtype.construct_array_type()
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,18 @@ def test_stack_timezone_aware_values():
tm.assert_series_equal(result, expected)


def test_stack_empty_frame():
tm.assert_series_equal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use

result=
expected=
tm.assert_series_equal (or frame)

pls parameterize these cases

add a comment with the issue number

DataFrame().stack(),
Series(index=MultiIndex([[], []], [[], []]), dtype=np.float64),
)
tm.assert_series_equal(
DataFrame().stack(dropna=True),
Series(index=MultiIndex([[], []], [[], []]), dtype=np.float64),
)
tm.assert_frame_equal(DataFrame().stack().unstack(), DataFrame())


def test_unstacking_multi_index_df():
# see gh-30740
df = DataFrame(
Expand Down