環境
- python 3.13.1
- pandas 2.3.2
- numpy 2.2.4
やりたいこと
pandasで以下のCSVを読み込んだ後、文字列が格納されている列はdtypeをobject
でなくstring
にしたいです。
sample.csv
id,name,path
1,n1,p1
2,n2,p2
path
列はオプショナルなので、path
列が存在しないCSVも読み込めるようにしたいです。
sample2.csv
id,name
1,n1
2,n2
分かったこと
pandas.read_csv
のdtype
引数でdtypeを変更したところ、存在しない列path
を指定しても、エラーは発生しませんでした。
In [34]: df = pd.read_csv("sample2.csv", dtype={"name":"string","path":"string"})
In [35]: df.dtypes
Out[35]:
id int64
name string[python]
dtype: object
しかし、あとからpandas.DataFrame.astype
でdtypeを変換する場合は、path
列が存在しないとエラーが発生しました。
In [36]: df = pd.read_csv("sample2.csv")
In [37]: df.dtypes
Out[37]:
id int64
name object
dtype: object
In [38]: df2 = df.astype({"name":"string","path":"string"})
---------------------------------------------------------------------------
...
KeyError: "Only a column name can be used for the key in a dtype mappings argument. 'path' not found in columns."
補足
pandas.read_csv
のusecols
引数は存在しない列を指定すると、エラーが発生しました。
In [39]: df=pd.read_csv("sample2.csv", usecols=["id","name","path"])
---------------------------------------------------------------------------
...
ValueError: Usecols do not match columns, columns expected but not found: ['path']