pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.81k stars 17.98k forks source link

ENH: Add a clear option to interpret strings as Pandas dtypes specifically. #59777

Open vitkosbence opened 2 months ago

vitkosbence commented 2 months ago

Feature Type

Problem Description

When the pd.read_excel function encounters an empty cell with a numeric dtype, it leads to a conversion error. The solution to this, is to use the Pandas specific nullable numeric types, which work extremely well. The problem is that these datatypes are hard to access via strings. pd.read_excel is especially keen on interpreting strings as numpy datatypes, while pd.api.types.pandas_dtype is capable of returning pandas dtypes, but it's unclear what this is based on.

Feature Description

Since the pandas dtypes often have several advantages over their numpy counterparts, it would make sense that functions which are capable of interpreting strings as datatypes have a flag, which forces them (if possible) to interpret strings as pandas datatypes and not their numpy counterparts.

Alternative Solutions

A workaround for now, is to run every datatype through the pd.api.types.pandas_dtype function, with very specific capitalization. Running: type(pd.api.types.pandas_dtype("Int64")) Returns: <class 'pandas.core.arrays.integer.Int64Dtype'> Which is good, but running: type(pd.api.types.pandas_dtype("int64")) Returns: <class 'numpy.dtypes.Int64DType'> So while this is technically a solution, trying to use the pandas interpreter for input from a user who doesn't have intricate knowledge of the issue is not possible.

Additional Context

No response

rhshadrach commented 2 months ago

Thanks for the report, does specifying pd.options.future.infer_string = True do the trick?

See https://pandas.pydata.org/pdeps/0014-string-dtype.html for more details.