Skip to content

BUG: DataFrame.__setitem__ creates object-dtype array for extension type scalars #34832

Closed
@justinessert

Description

@justinessert
  • 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 pandas as pd

df = pd.DataFrame({
    'a': [1, 2]
})

df['b'] = pd.Period('2020-01')
df['c'] = df['b'] + 0
df['d'] = df['a'] + df['b']

df.dtypes

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 a period[M]. Stranger to me still is that both df['c'] and df['d'] are correctly set to period[M] columns. This seems very odd to me, that when you combine an object column with a int, you get a period[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 a period[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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugExtensionArrayExtending pandas with custom dtypes or arrays.IndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions