ENH: Add support for dataclasses in the DataFrame constructor
State: closed

Added support for data-classes when used in the construction of a new dataframe.

- [X] closes #21910
- [X] tests added / passed
- [X] passes `black pandas`
- [X] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

Simply put, added support to use dataclasses in the following way:

```python
from dataclasses import dataclass

@dataclass 
class Person:
    name: str
    age: int

df = DataFrame([Person("me", 25), Person("you", 35)])
```

Labels:
name: API Design
name: Constructors
description: Series/DataFrame/Index/pd.array Constructors

Comments:
Hello @asosnovsky! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:











There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-03-14 23:03:57 UTC
> Needs a release note (1.0.rst).
> 
> A few edge cases we should have tests for
> 
>     1. A mixture of dataclass types  (probably takes the union of all the columns?)
> 
>     2. A mixture of dataclass and non-dataclass (this probably raises. Can we ensure that the error message is reasonable)

0. I updated the unit-tests for these cases [see here](https://github.com/asosnovsky/pandas/blob/issue/21910/pandas/tests/frame/test_constructors.py#L1340)

1. since the evaluation of dataclasses simply latches onto that of dictionaries, mixing dataclasses would result in more columns ([see this](https://github.com/asosnovsky/pandas/blob/issue/21910/pandas/tests/frame/test_constructors.py#L1355)).

2. A mixture of none-dataclasses and dataclasses will result in a TypeError [see here](https://github.com/asosnovsky/pandas/blob/issue/21910/pandas/tests/frame/test_constructors.py#L1364) I also added a note in the documentation about this [link](https://github.com/asosnovsky/pandas/blob/issue/21910/doc/source/getting_started/dsintro.rst#from-a-list-of-dataclasses).


@TomAugspurger Hi, I am having difficulties reading the logs and understanding where the failure is for these, could I get some help. It will be greatly appreciated :) 
> Not entirely sure about the doc build failure yet.
@TomAugspurger 
> 
> If you merge master and repush, the codecov failure on travis will go away.
First of, thank you for the help!! :)

I applied your previous comments and re-merged with master. Now I got only 2 failures, going to take a look at it. 
----------- Edit ---------

I think I got the bug, I appear to have been overwriting a variable `data` which was used in a previous example and is re-used in a subsequent example. Hopefully this will be the final commit for this.
this needs a perf check to make sure no regressions
@asosnovsky can you merge master and we'll have another look.
@asosnovsky can you rebase and we'll see why the linter failed
@asosnovsky this looks close; logs aren't available from failure but can you merge master and repush to see if that solves?
> @asosnovsky this looks close; logs aren't available from failure but can you merge master and repush to see if that solves?

sorry it took so long, got really busy at work, merged with master and pushed again.
would be a nice change, can you merge master and ping on green.
Hi @asosnovsky  - sorry to chase you up, just wanted to ask whether you're still working on this :)
Hi @asosnovsky , is this active? I'm hesitant to close this as stale as it looks like it's almost done, but we haven't heard from you in over 2 months
this looks fine, but needs a rebase if someone wants to merge master. ping on green.
@jreback green
thanks @asosnovsky 
Thanks @asosnovsky Very nice. a few comments to follow-up.

1. I think we should have some mention of this enhancement a whatsnew.
2. #21910 also suggested a to_dataclasses method so maybe should not be closed, or open another issue to discuss this further.
3. Would it make sense to also add dataclass support to the Series constructor for consistency? although the field names would now be the index and dtype would be either object to maintain the field types or maybe coerced?
> Thanks @asosnovsky Very nice. a few comments to follow-up.
> 
> 
> 
> 1. I think we should have some mention of this enhancement a whatsnew.
> 
> 2. #21910 also suggested a to_dataclasses method so maybe should not be closed, or open another issue to discuss this further.
> 
> 3. Would it make sense to also add dataclass support to the Series constructor for consistency? although the field names would now be the index and dtype would be either object to maintain the field types or maybe coerced?

Not sure if it makes sense to add a "to_dataclasses" method. Would you have to create a dataclass on the fly or feed in one?

Not sure about series either. But if we were to do that, don't series need to have a single dtype? What happens to a dataclass with a mixed bag of types ?
I think this feature is quite cool and worth mentioning here:
https://github.com/pandas-dev/pandas/blob/master/doc/source/whatsnew/v1.1.0.rst
hmm this originally did have a whats new note ; must have gotten lost

would take a patch to add one
@asosnovsky please do that. I'm not a native speaker and I think it'll be easier for you to describe the change :)