Skip to content

DOC: DataFrame.unstack should accept fill_value with more types than just int/str/dict #61445

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

Open
1 task done
loicdiridollou opened this issue May 16, 2025 · 1 comment
Open
1 task done
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@loicdiridollou
Copy link
Member

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.unstack.html#pandas.DataFrame.unstack

Documentation problem

Currently the docs stipulate that only int, str and dict are allowed for the fill_value, yet it seems like all the types that could be used when creating a DataFrame seem to pass at runtime. I have not tried them all yet but int, float, complex, timestamp are working fine.

Suggested fix for documentation

Add all allowed types for dataframe elements for the fill_value field.
Happy to create the PR if this is agreed by the maintainers. I will raise the issue in the pandas-stubs repo.

@loicdiridollou
Copy link
Member Author

loicdiridollou commented May 16, 2025

Also I realized that the dict option is quite confusing, I am not able to make it work, the following code will fail:

pd.DataFrame([
    ["a", "b", pd.Timestamp(2021, 3, 2)],
    ["a", "a", pd.Timestamp(2023, 4, 2)],
    ["b", "b", pd.Timestamp(2024, 3, 2)]
]).set_index([0, 1]).unstack(1, fill_value={2: pd.Timestamp(2023, 4, 5)})
  File "/Users/loic/Documents/Code/pandas-stubs/gh1214_unstack/.venv/lib/python3.13/site-packages/pandas/core/reshape/reshape.py", line 238, in get_result
    values, _ = self.get_new_values(values, fill_value)
                ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Users/loic/Documents/Code/pandas-stubs/gh1214_unstack/.venv/lib/python3.13/site-packages/pandas/core/reshape/reshape.py", line 288, in get_new_values
    dtype, fill_value = maybe_promote(dtype, fill_value)
                        ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/loic/Documents/Code/pandas-stubs/gh1214_unstack/.venv/lib/python3.13/site-packages/pandas/core/dtypes/cast.py", line 595, in maybe_promote
    dtype, fill_value = _maybe_promote(dtype, fill_value)
                        ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/loic/Documents/Code/pandas-stubs/gh1214_unstack/.venv/lib/python3.13/site-packages/pandas/core/dtypes/cast.py", line 622, in _maybe_promote
    raise ValueError("fill_value must be a scalar")
ValueError: fill_value must be a scalar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant