Skip to content
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

BUG: DataFrame constructor with UserList dataclass broken by upgrade to 1.1.0 #41682

Open
2 of 3 tasks
TomGoBravo opened this issue May 26, 2021 · 2 comments
Open
2 of 3 tasks
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Regression Functionality that used to work in a prior pandas version

Comments

@TomGoBravo
Copy link

TomGoBravo commented May 26, 2021

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • (optional) I have confirmed this bug exists on the master branch of pandas.

Code Sample, a copy-pastable example

import dataclasses
import pandas as pd
from typing import List
from collections import UserList

@dataclasses.dataclass(frozen=True)
class MyList(UserList):
  data: List[float]


stuff = [MyList([1, 2, 3]), [4, 5, 6]]

df = pd.DataFrame(stuff)
print("No error!")

Problem description

I wrote code like this for pandas 1.0.5. When I upgraded to 1.2 my unittests started failing with TypeError: asdict() should be called on dataclass instances. PR #27999 and issue #21910 changed the behavior of the DataFrame constructor to treat data as all dataclasses if the first element is a dataclass. The behavior is documented but not quite what I'd like.

Expected Output

Admittedly a esoteric case, but I'm documenting my upgrade issuse. In this case I think checking for is_list_like(data[0]) before is_dataclass(data[0]) works better for me. Or maybe in hindsight from_dataclasses wasn't a bad idea.

@TomGoBravo TomGoBravo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 26, 2021
TomGoBravo pushed a commit to act-now-coalition/covid-data-model that referenced this issue May 27, 2021
Upgrading pandas introduced `TypeError: asdict() should be called on dataclass instances` as documented at pandas-dev/pandas#41682

This PR is a work-around and maybe makes build_dataset slightly easier to read.
@jbrockmendel jbrockmendel added Constructors Series/DataFrame/Index/pd.array Constructors Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jun 7, 2021
@jbrockmendel
Copy link
Member

This is the UserList from the stdlib collections?

@TomGoBravo
Copy link
Author

This is the UserList from the stdlib collections?

Ooops, yes. I updated the example above.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

4 participants