Description
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
# I add attributes to a DataFrame
df = pd.DataFrame(dict(column1=[1,2,3,4], column2=[1,2,3,4]))
df.attrs['name'] = "hello"
print(df)
print(df.attrs)
OUT:
column1 column2
0 1 1
1 2 2
2 3 3
3 4 4
{'name': 'hello'}
# When I change the dtype, the attribute is still there (as expected)
df1 = df.astype(float)
print(df1)
print(df1.attrs)
OUT:
column1 column2
0 1.0 1.0
1 2.0 2.0
2 3.0 3.0
3 4.0 4.0
{'name': 'hello'}
# But when I specify the dtype for specific columns, the attributes are lost
df2 = df.astype({'column1':float})
print(df2)
print(df2.attrs)
OUT:
column1 column2
0 1.0 1
1 2.0 2
2 3.0 3
3 4.0 4
{}
Issue Description
I assigned attributes to a DataFrame. When I change the DataFrame dtype with df.astype(float)
, the DataFrame attribute are retained, as expected. But when I change the dtype for specific columns with df.astype({'column1':float})
, the attributes are lost.
Expected Behavior
I expected the DataFrame to retrain the attributes after specifying the dtype for specific columns as it does when I specify the dtype for the full DataFrame.
Installed Versions
INSTALLED VERSIONS
commit : 945c9ed
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.18-24.78-default
Version : #1 SMP Mon Aug 2 21:00:23 UTC 2021 (a13100d)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.2.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.0
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fsspec : 2021.10.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : 2021.10.1
scipy : 1.7.1
sqlalchemy : 1.4.26
tables : None
tabulate : None
xarray : 0.19.0
xlrd : None
xlwt : None
numba : None