Skip to content

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

Open
@TomGoBravo

Description

@TomGoBravo
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions