Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [ ] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# empty df with columns dtypes float and object
a = pd.DataFrame(columns=["test", "w"])
a["test"] = a["test"].astype(float)
a["w"] = a["w"].astype(object)

# df with one row. also with columns dtypes float and object
b = pd.DataFrame({"test": [42.], "w": ["a"]})

# copying b.loc[0] to a.loc[0] makes all dtypes object
a.loc[0] = b.loc[0]
print(a.dtypes)
# output is:
"""
test    object
w       object
dtype: object
"""
# expected output (tested with pandas 1.1 and 1.3.):
"""
test    float64
w        object
dtype: object
"""

Issue Description

copying a df.loc[index] with columns containing dtype object to another df forces all dtypes to object. the output is:

test object w object dtype: object

Expected Behavior

Each column's dtype is identical to the original dtype. This is the case with older pandas versions (tested with 1.1 and 1.3). Since pandas 1.4 this behavious has changed.

expected output: test float64 w object dtype: object

Installed Versions

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.8.15.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.17763 machine : AMD64 processor : AMD64 Family 25 Model 1 Stepping 1, AuthenticAMD byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252 pandas : 1.5.2 numpy : 1.23.5 pytz : 2022.6 dateutil : 2.8.2 setuptools : 65.5.0 pip : 22.2.2 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

Comment From: phofl

Can you trim this down to clearly show what does not work? This is very confusing the way you prepare the example. Just create the DataFrame and show the output you would expect

Comment From: jankaeh

I updated the issue (don't know if you get notified). I hope that helps.

Comment From: phofl

Updates don't notify, so thx for pinging. Yes this is better!

Comment From: phofl

This has nothing to do with .copy() though

Comment From: phofl

This is not a bug, b.loc[0] converts your row to a Series, which uses a common dtype to hold all values, which is object.

Comment From: jankaeh

ok, so this basically was a bug in the older versions then... a kind of convenient one ;). thanks