read_csv 的 dtype 控制
中等
22-pandas-only pandas 专属
iocsvdtypena-values
工作目录里有一个 CSV 文件 data/raw.csv(带表头),列为 id, zip, region, signup_date, amount, comment。
请用一次读取把它读成类型正确的表:
- 只要
id, zip, region, signup_date, amount 这五列,comment 不要。
id:整数。
zip:邮编,必须是字符串,保留前导零(例如 02139 不能变成 2139)。
region:字符串,空单元格为 null。
signup_date:日期(格式 YYYY-MM-DD),空单元格为 null。
amount:浮点数。文件里用 - 和 ? 表示金额缺失,这两种都要读成 null;0.00 是真实的 0,不是缺失。
输出
列为 id, zip, region, signup_date, amount,文件里每行数据对应一行,行顺序不限。
工作目录里的文件 (case base)
data/raw.csv
id,zip,region,signup_date,amount,comment
1,02139,east,2026-01-05,120.50,first order
2,10001,east,2026-01-07,-,"called twice, no answer"
3,94105,west,2026-02-11,88.00,
4,00501,east,2026-02-28,?,vip
5,60601,central,2026-03-01,0.00,"refund, then reorder"
6,98101,west,2026-03-15,310.25,
7,07030,east,2026-04-02,-,
8,73301,central,2026-04-30,45.10,promo
共 9 行
期望输出 (case base,行顺序不限)
| # | idInt64 | zipString | regionString | signup_dateDatetime(time_unit='us', time_zone=None) | amountFloat64 |
| 1 |
1 |
02139 |
east |
2026-01-05 00:00:00 |
120.5 |
| 2 |
2 |
10001 |
east |
2026-01-07 00:00:00 |
null |
| 3 |
3 |
94105 |
west |
2026-02-11 00:00:00 |
88.0 |
| 4 |
4 |
00501 |
east |
2026-02-28 00:00:00 |
null |
| 5 |
5 |
60601 |
central |
2026-03-01 00:00:00 |
0.0 |
| 6 |
6 |
98101 |
west |
2026-03-15 00:00:00 |
310.25 |
| 7 |
7 |
07030 |
east |
2026-04-02 00:00:00 |
null |
| 8 |
8 |
73301 |
central |
2026-04-30 00:00:00 |
45.1 |
共 8 行