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
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