-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.__setitem__ creates object-dtype array for extension type scalars #34832
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
Comments
Slightly related to #31763. Bug is somewhere in Lines 3731 to 3734 in 5fdd6f5
cast_scalar_to_array pandas/pandas/core/dtypes/cast.py Line 1491 in 5fdd6f5
(Pdb) value
Period('2020-01', 'M')
(Pdb) pp infer_dtype
period[M]
(Pdb) pp cast_scalar_to_array(len(self.index), value)
array([Period('2020-01', 'M'), Period('2020-01', 'M')], dtype=object) Ideally we would recognize that as an extension dtype and create the desired array with (Pdb) pp infer_dtype.construct_array_type()._from_sequence([value] * len(self.index))
<PeriodArray>
['2020-01', '2020-01']
Length: 2, dtype: period[M] Further investigation / PRs to fix this would be very welcome. Further investigation |
pls try on master |
Confirmed that's the same behavior on master. |
Thank you @TomAugspurger for the lead! Looking into this issue a little further I found two notable issues that cause this (not sure how much I'm just repeating @TomAugspurger): Issue 1: Issue 2: The To correctly handle this bug, the function would have to return a Period Array (is this what you are referring to as an extension array?) via the code @TomAugspurger provided above. Is this an acceptable way of solving this issue? I believe this fix would also require some code do auto-detect whether a scalar value is a Period type, so that the function knows whether to return an extension array or a numpy array. Is that something that exists currently? |
Yeah. So |
@justinessert #34875 fixed this and added tests. This issue can be closed? |
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
Problem description
Not sure if this is best classified as a bug, but wasn't sure how to classify it since it certainly defies my expectations of how the Pandas package would work and would like to understand what's happening.
Essentially the issue is that, in the code snippet above,
df['b']
's is considered an object rather than aperiod[M]
. Stranger to me still is that bothdf['c'] and df['d']
are correctly set toperiod[M]
columns. This seems very odd to me, that when you combine anobject
column with aint
, you get aperiod[M]
column.Can you help me understand what the logic is here? Is it possible to update Pandas so that when someone does
df['b'] = pd.Period('2020-01')
the resulting column is aperiod[M]
type?Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.4.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.4
numpy : 1.18.5
pytz : 2019.1
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : None
pytest : 4.4.1
hypothesis : None
sphinx : 2.4.2
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.4.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.0
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 4.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.2.8
numba : None
The text was updated successfully, but these errors were encountered: