0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

`pandas.read_csv`の`dtype`引数では、存在しない列を指定してもエラーが発生しない

Last updated at Posted at 2025-09-12

環境

  • 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_csvdtype引数で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_csvusecols引数は存在しない列を指定すると、エラーが発生しました。

In [39]: df=pd.read_csv("sample2.csv", usecols=["id","name","path"])
---------------------------------------------------------------------------
...
ValueError: Usecols do not match columns, columns expected but not found: ['path']
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?