Using Series in DataFrame.astype() does not work correctly (original) (raw)

Code Sample

import pandas as pd import numpy as np

df = pd.DataFrame([[1,2],[3,4]], columns=["A", "B"])

df

 A 	B

0 1 2 1 3 4

df.astype({"A": np.float64, "B": np.float64})

 A 	B

0 1.0 2.0 1 3.0 4.0

df.astype(pd.Series({"A": np.float64, "B": np.float64})) # using a series does not react at all (error or right behaviour) A B 0 1 2 1 3 4

df.astype(list(pd.Series({"A": np.float64, "B": np.float64}))) # using a type that is not allowed raises error [...] TypeError: data type not understood

Problem description

I wanted to parse the columns of a DF with a Series (instead of a dict as stated in the docs). I realised that the types did not change at all nor an error was raised. Casting to dict helped (see in code).

Expected Output

I expect to give the same error as when a wrong data type (e.g. list) is used, or alternatively that it works the same way as a dict. At the moment nothing happens.

Detailspandas: 0.20.2 pytest: None pip: 9.0.1 setuptools: 28.8.0 Cython: None numpy: 1.13.0 scipy: None xarray: None IPython: 6.1.0 sphinx: None patsy: None dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.0.2 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None