Code Sample, a copy-pastable example if possible
# Your code here
df = pd.DataFrame({"a":[1,2,3], "b":["x", "y", "z"]})
df.astype({"a":str}, copy=True)
df.astype({"a":str}, copy=False)
print(df.dtypes)
Problem description
no matter what I give copy True or False. Dtype of \<column a> will not be converted. the output is:
a int64 b object
Expected Output
a object b object
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.21.1
pytest: None
pip: 9.0.1
setuptools: 28.8.0
Cython: None
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
Comment From: jschendel
astype
is not an inplace operation so you'll need to reassign, i.e. df = df.astype({"a": str})
. Doing so produces the expected output:
In [2]: df = pd.DataFrame({"a":[1,2,3], "b":["x", "y", "z"]})
In [3]: df = df.astype({"a": str})
In [4]: df.dtypes
Out[4]:
a object
b object
dtype: object
In [5]: pd.__version__
Out[5]: '0.21.1'
Comment From: scotthuang1989
yes. I now use a reassign in my program. But what is the function of this parameter?
Comment From: raffaem
Hi, also asking what does the copy
parameter do.
Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).
"returns a copy" of what? and what is returned with copy=False
?