ENH: Dataframe constructor and DataFrame.from_records shoudl allow specifying column dtypes #46868
Labels
Closing Candidate
May be closeable, needs more eyeballs
DataFrame
DataFrame data structure
Enhancement
Is your feature request related to a problem?
When creating a
DataFrame
from a list of row tuples using either the constructor orDataFrame.from_rows()
, I often run into issues where the column data type is not inferred correctly. In my specific case I have a mix ofint
s andNone
s and would like to have anobject
dtype, but pandas wants to convert tofloat
.Describe the solution you'd like
read_csv()
has adtype
argument that allows setting individual column data types, I think these two methods should have something similar. The constructor does already have adtype
argument but it accepts only a single value, and tabular data is very frequently not of a uniform data type. Because thecolumns
argument is optional it should probably accept a list/sequence ofdtype
s instead of just a dict keyed by column names as inread_csv()
.API breaking implications
None, this would add a new optional argument to
DataFrame.from_records()
and expand the allowed values of thedtype
argument to the constructor without changing the interpretation of currently accepted values.Describe alternatives you've considered
may work in a lot of cases, but mine requires an ugly list comprehension to undo the float conversion. Being able to manually specify the column data types seems like a no-brainer.
The text was updated successfully, but these errors were encountered: