Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

1adc667d-d642-4ba8-a9f4-fe4898d69985 17 KB
Raw

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  1. """
  2. Tests that NA values are properly handled during
  3. parsing for all of the parsers defined in parsers.py
  4. """
  5. from io import StringIO
  6. import numpy as np
  7. import pytest
  8. from pandas._libs.parsers import STR_NA_VALUES
  9. from pandas import (
  10. DataFrame,
  11. Index,
  12. MultiIndex,
  13. )
  14. import pandas._testing as tm
  15. skip_pyarrow = pytest.mark.usefixtures("pyarrow_skip")
  16. xfail_pyarrow = pytest.mark.usefixtures("pyarrow_xfail")
  17. def test_string_nas(all_parsers):
  18. parser = all_parsers
  19. data = """A,B,C
  20. a,b,c
  21. d,,f
  22. ,g,h
  23. """
  24. result = parser.read_csv(StringIO(data))
  25. expected = DataFrame(
  26. [["a", "b", "c"], ["d", np.nan, "f"], [np.nan, "g", "h"]],
  27. columns=["A", "B", "C"],
  28. )
  29. if parser.engine == "pyarrow":
  30. expected.loc[2, "A"] = None
  31. expected.loc[1, "B"] = None
  32. tm.assert_frame_equal(result, expected)
  33. def test_detect_string_na(all_parsers):
  34. parser = all_parsers
  35. data = """A,B
  36. foo,bar
  37. NA,baz
  38. NaN,nan
  39. """
  40. expected = DataFrame(
  41. [["foo", "bar"], [np.nan, "baz"], [np.nan, np.nan]], columns=["A", "B"]
  42. )
  43. if parser.engine == "pyarrow":
  44. expected.loc[[1, 2], "A"] = None
  45. expected.loc[2, "B"] = None
  46. result = parser.read_csv(StringIO(data))
  47. tm.assert_frame_equal(result, expected)
  48. @skip_pyarrow
  49. @pytest.mark.parametrize(
  50. "na_values",
  51. [
  52. ["-999.0", "-999"],
  53. [-999, -999.0],
  54. [-999.0, -999],
  55. ["-999.0"],
  56. ["-999"],
  57. [-999.0],
  58. [-999],
  59. ],
  60. )
  61. @pytest.mark.parametrize(
  62. "data",
  63. [
  64. """A,B
  65. -999,1.2
  66. 2,-999
  67. 3,4.5
  68. """,
  69. """A,B
  70. -999,1.200
  71. 2,-999.000
  72. 3,4.500
  73. """,
  74. ],
  75. )
  76. def test_non_string_na_values(all_parsers, data, na_values):
  77. # see gh-3611: with an odd float format, we can't match
  78. # the string "999.0" exactly but still need float matching
  79. parser = all_parsers
  80. expected = DataFrame([[np.nan, 1.2], [2.0, np.nan], [3.0, 4.5]], columns=["A", "B"])
  81. result = parser.read_csv(StringIO(data), na_values=na_values)
  82. tm.assert_frame_equal(result, expected)
  83. def test_default_na_values(all_parsers):
  84. _NA_VALUES = {
  85. "-1.#IND",
  86. "1.#QNAN",
  87. "1.#IND",
  88. "-1.#QNAN",
  89. "#N/A",
  90. "N/A",
  91. "n/a",
  92. "NA",
  93. "<NA>",
  94. "#NA",
  95. "NULL",
  96. "null",
  97. "NaN",
  98. "nan",
  99. "-NaN",
  100. "-nan",
  101. "#N/A N/A",
  102. "",
  103. "None",
  104. }
  105. assert _NA_VALUES == STR_NA_VALUES
  106. parser = all_parsers
  107. nv = len(_NA_VALUES)
  108. def f(i, v):
  109. if i == 0:
  110. buf = ""
  111. elif i > 0:
  112. buf = "".join([","] * i)
  113. buf = f"{buf}{v}"
  114. if i < nv - 1:
  115. joined = "".join([","] * (nv - i - 1))
  116. buf = f"{buf}{joined}"
  117. return buf
  118. data = StringIO("\n".join([f(i, v) for i, v in enumerate(_NA_VALUES)]))
  119. expected = DataFrame(np.nan, columns=range(nv), index=range(nv))
  120. result = parser.read_csv(data, header=None)
  121. tm.assert_frame_equal(result, expected)
  122. # TODO: needs skiprows list support in pyarrow
  123. @skip_pyarrow
  124. @pytest.mark.parametrize("na_values", ["baz", ["baz"]])
  125. def test_custom_na_values(all_parsers, na_values):
  126. parser = all_parsers
  127. data = """A,B,C
  128. ignore,this,row
  129. 1,NA,3
  130. -1.#IND,5,baz
  131. 7,8,NaN
  132. """
  133. expected = DataFrame(
  134. [[1.0, np.nan, 3], [np.nan, 5, np.nan], [7, 8, np.nan]], columns=["A", "B", "C"]
  135. )
  136. result = parser.read_csv(StringIO(data), na_values=na_values, skiprows=[1])
  137. tm.assert_frame_equal(result, expected)
  138. def test_bool_na_values(all_parsers):
  139. data = """A,B,C
  140. True,False,True
  141. NA,True,False
  142. False,NA,True"""
  143. parser = all_parsers
  144. result = parser.read_csv(StringIO(data))
  145. expected = DataFrame(
  146. {
  147. "A": np.array([True, np.nan, False], dtype=object),
  148. "B": np.array([False, True, np.nan], dtype=object),
  149. "C": [True, False, True],
  150. }
  151. )
  152. if parser.engine == "pyarrow":
  153. expected.loc[1, "A"] = None
  154. expected.loc[2, "B"] = None
  155. tm.assert_frame_equal(result, expected)
  156. # TODO: Needs pyarrow support for dictionary in na_values
  157. @skip_pyarrow
  158. def test_na_value_dict(all_parsers):
  159. data = """A,B,C
  160. foo,bar,NA
  161. bar,foo,foo
  162. foo,bar,NA
  163. bar,foo,foo"""
  164. parser = all_parsers
  165. df = parser.read_csv(StringIO(data), na_values={"A": ["foo"], "B": ["bar"]})
  166. expected = DataFrame(
  167. {
  168. "A": [np.nan, "bar", np.nan, "bar"],
  169. "B": [np.nan, "foo", np.nan, "foo"],
  170. "C": [np.nan, "foo", np.nan, "foo"],
  171. }
  172. )
  173. tm.assert_frame_equal(df, expected)
  174. @pytest.mark.parametrize(
  175. "index_col,expected",
  176. [
  177. (
  178. [0],
  179. DataFrame({"b": [np.nan], "c": [1], "d": [5]}, index=Index([0], name="a")),
  180. ),
  181. (
  182. [0, 2],
  183. DataFrame(
  184. {"b": [np.nan], "d": [5]},
  185. index=MultiIndex.from_tuples([(0, 1)], names=["a", "c"]),
  186. ),
  187. ),
  188. (
  189. ["a", "c"],
  190. DataFrame(
  191. {"b": [np.nan], "d": [5]},
  192. index=MultiIndex.from_tuples([(0, 1)], names=["a", "c"]),
  193. ),
  194. ),
  195. ],
  196. )
  197. def test_na_value_dict_multi_index(all_parsers, index_col, expected):
  198. data = """\
  199. a,b,c,d
  200. 0,NA,1,5
  201. """
  202. parser = all_parsers
  203. result = parser.read_csv(StringIO(data), na_values=set(), index_col=index_col)
  204. tm.assert_frame_equal(result, expected)
  205. # TODO: xfail components of this test, the first one passes
  206. @skip_pyarrow
  207. @pytest.mark.parametrize(
  208. "kwargs,expected",
  209. [
  210. (
  211. {},
  212. DataFrame(
  213. {
  214. "A": ["a", "b", np.nan, "d", "e", np.nan, "g"],
  215. "B": [1, 2, 3, 4, 5, 6, 7],
  216. "C": ["one", "two", "three", np.nan, "five", np.nan, "seven"],
  217. }
  218. ),
  219. ),
  220. (
  221. {"na_values": {"A": [], "C": []}, "keep_default_na": False},
  222. DataFrame(
  223. {
  224. "A": ["a", "b", "", "d", "e", "nan", "g"],
  225. "B": [1, 2, 3, 4, 5, 6, 7],
  226. "C": ["one", "two", "three", "nan", "five", "", "seven"],
  227. }
  228. ),
  229. ),
  230. (
  231. {"na_values": ["a"], "keep_default_na": False},
  232. DataFrame(
  233. {
  234. "A": [np.nan, "b", "", "d", "e", "nan", "g"],
  235. "B": [1, 2, 3, 4, 5, 6, 7],
  236. "C": ["one", "two", "three", "nan", "five", "", "seven"],
  237. }
  238. ),
  239. ),
  240. (
  241. {"na_values": {"A": [], "C": []}},
  242. DataFrame(
  243. {
  244. "A": ["a", "b", np.nan, "d", "e", np.nan, "g"],
  245. "B": [1, 2, 3, 4, 5, 6, 7],
  246. "C": ["one", "two", "three", np.nan, "five", np.nan, "seven"],
  247. }
  248. ),
  249. ),
  250. ],
  251. )
  252. def test_na_values_keep_default(all_parsers, kwargs, expected):
  253. data = """\
  254. A,B,C
  255. a,1,one
  256. b,2,two
  257. ,3,three
  258. d,4,nan
  259. e,5,five
  260. nan,6,
  261. g,7,seven
  262. """
  263. parser = all_parsers
  264. result = parser.read_csv(StringIO(data), **kwargs)
  265. tm.assert_frame_equal(result, expected)
  266. def test_no_na_values_no_keep_default(all_parsers):
  267. # see gh-4318: passing na_values=None and
  268. # keep_default_na=False yields 'None" as a na_value
  269. data = """\
  270. A,B,C
  271. a,1,None
  272. b,2,two
  273. ,3,None
  274. d,4,nan
  275. e,5,five
  276. nan,6,
  277. g,7,seven
  278. """
  279. parser = all_parsers
  280. result = parser.read_csv(StringIO(data), keep_default_na=False)
  281. expected = DataFrame(
  282. {
  283. "A": ["a", "b", "", "d", "e", "nan", "g"],
  284. "B": [1, 2, 3, 4, 5, 6, 7],
  285. "C": ["None", "two", "None", "nan", "five", "", "seven"],
  286. }
  287. )
  288. tm.assert_frame_equal(result, expected)
  289. # TODO: Blocked on na_values dict support in pyarrow
  290. @skip_pyarrow
  291. def test_no_keep_default_na_dict_na_values(all_parsers):
  292. # see gh-19227
  293. data = "a,b\n,2"
  294. parser = all_parsers
  295. result = parser.read_csv(
  296. StringIO(data), na_values={"b": ["2"]}, keep_default_na=False
  297. )
  298. expected = DataFrame({"a": [""], "b": [np.nan]})
  299. tm.assert_frame_equal(result, expected)
  300. # TODO: Blocked on na_values dict support in pyarrow
  301. @skip_pyarrow
  302. def test_no_keep_default_na_dict_na_scalar_values(all_parsers):
  303. # see gh-19227
  304. #
  305. # Scalar values shouldn't cause the parsing to crash or fail.
  306. data = "a,b\n1,2"
  307. parser = all_parsers
  308. df = parser.read_csv(StringIO(data), na_values={"b": 2}, keep_default_na=False)
  309. expected = DataFrame({"a": [1], "b": [np.nan]})
  310. tm.assert_frame_equal(df, expected)
  311. # TODO: Blocked on na_values dict support in pyarrow
  312. @skip_pyarrow
  313. @pytest.mark.parametrize("col_zero_na_values", [113125, "113125"])
  314. def test_no_keep_default_na_dict_na_values_diff_reprs(all_parsers, col_zero_na_values):
  315. # see gh-19227
  316. data = """\
  317. 113125,"blah","/blaha",kjsdkj,412.166,225.874,214.008
  318. 729639,"qwer","",asdfkj,466.681,,252.373
  319. """
  320. parser = all_parsers
  321. expected = DataFrame(
  322. {
  323. 0: [np.nan, 729639.0],
  324. 1: [np.nan, "qwer"],
  325. 2: ["/blaha", np.nan],
  326. 3: ["kjsdkj", "asdfkj"],
  327. 4: [412.166, 466.681],
  328. 5: ["225.874", ""],
  329. 6: [np.nan, 252.373],
  330. }
  331. )
  332. result = parser.read_csv(
  333. StringIO(data),
  334. header=None,
  335. keep_default_na=False,
  336. na_values={2: "", 6: "214.008", 1: "blah", 0: col_zero_na_values},
  337. )
  338. tm.assert_frame_equal(result, expected)
  339. # TODO: Empty null_values doesn't work properly on pyarrow
  340. @skip_pyarrow
  341. @pytest.mark.parametrize(
  342. "na_filter,row_data",
  343. [
  344. (True, [[1, "A"], [np.nan, np.nan], [3, "C"]]),
  345. (False, [["1", "A"], ["nan", "B"], ["3", "C"]]),
  346. ],
  347. )
  348. def test_na_values_na_filter_override(all_parsers, na_filter, row_data):
  349. data = """\
  350. A,B
  351. 1,A
  352. nan,B
  353. 3,C
  354. """
  355. parser = all_parsers
  356. result = parser.read_csv(StringIO(data), na_values=["B"], na_filter=na_filter)
  357. expected = DataFrame(row_data, columns=["A", "B"])
  358. tm.assert_frame_equal(result, expected)
  359. # TODO: Arrow parse error
  360. @skip_pyarrow
  361. def test_na_trailing_columns(all_parsers):
  362. parser = all_parsers
  363. data = """Date,Currency,Symbol,Type,Units,UnitPrice,Cost,Tax
  364. 2012-03-14,USD,AAPL,BUY,1000
  365. 2012-05-12,USD,SBUX,SELL,500"""
  366. # Trailing columns should be all NaN.
  367. result = parser.read_csv(StringIO(data))
  368. expected = DataFrame(
  369. [
  370. ["2012-03-14", "USD", "AAPL", "BUY", 1000, np.nan, np.nan, np.nan],
  371. ["2012-05-12", "USD", "SBUX", "SELL", 500, np.nan, np.nan, np.nan],
  372. ],
  373. columns=[
  374. "Date",
  375. "Currency",
  376. "Symbol",
  377. "Type",
  378. "Units",
  379. "UnitPrice",
  380. "Cost",
  381. "Tax",
  382. ],
  383. )
  384. tm.assert_frame_equal(result, expected)
  385. # TODO: xfail the na_values dict case
  386. @skip_pyarrow
  387. @pytest.mark.parametrize(
  388. "na_values,row_data",
  389. [
  390. (1, [[np.nan, 2.0], [2.0, np.nan]]),
  391. ({"a": 2, "b": 1}, [[1.0, 2.0], [np.nan, np.nan]]),
  392. ],
  393. )
  394. def test_na_values_scalar(all_parsers, na_values, row_data):
  395. # see gh-12224
  396. parser = all_parsers
  397. names = ["a", "b"]
  398. data = "1,2\n2,1"
  399. result = parser.read_csv(StringIO(data), names=names, na_values=na_values)
  400. expected = DataFrame(row_data, columns=names)
  401. tm.assert_frame_equal(result, expected)
  402. @skip_pyarrow
  403. def test_na_values_dict_aliasing(all_parsers):
  404. parser = all_parsers
  405. na_values = {"a": 2, "b": 1}
  406. na_values_copy = na_values.copy()
  407. names = ["a", "b"]
  408. data = "1,2\n2,1"
  409. expected = DataFrame([[1.0, 2.0], [np.nan, np.nan]], columns=names)
  410. result = parser.read_csv(StringIO(data), names=names, na_values=na_values)
  411. tm.assert_frame_equal(result, expected)
  412. tm.assert_dict_equal(na_values, na_values_copy)
  413. @skip_pyarrow
  414. def test_na_values_dict_col_index(all_parsers):
  415. # see gh-14203
  416. data = "a\nfoo\n1"
  417. parser = all_parsers
  418. na_values = {0: "foo"}
  419. result = parser.read_csv(StringIO(data), na_values=na_values)
  420. expected = DataFrame({"a": [np.nan, 1]})
  421. tm.assert_frame_equal(result, expected)
  422. @skip_pyarrow
  423. @pytest.mark.parametrize(
  424. "data,kwargs,expected",
  425. [
  426. (
  427. str(2**63) + "\n" + str(2**63 + 1),
  428. {"na_values": [2**63]},
  429. DataFrame([str(2**63), str(2**63 + 1)]),
  430. ),
  431. (str(2**63) + ",1" + "\n,2", {}, DataFrame([[str(2**63), 1], ["", 2]])),
  432. (str(2**63) + "\n1", {"na_values": [2**63]}, DataFrame([np.nan, 1])),
  433. ],
  434. )
  435. def test_na_values_uint64(all_parsers, data, kwargs, expected):
  436. # see gh-14983
  437. parser = all_parsers
  438. result = parser.read_csv(StringIO(data), header=None, **kwargs)
  439. tm.assert_frame_equal(result, expected)
  440. def test_empty_na_values_no_default_with_index(all_parsers):
  441. # see gh-15835
  442. data = "a,1\nb,2"
  443. parser = all_parsers
  444. expected = DataFrame({"1": [2]}, index=Index(["b"], name="a"))
  445. result = parser.read_csv(StringIO(data), index_col=0, keep_default_na=False)
  446. tm.assert_frame_equal(result, expected)
  447. # TODO: Missing support for na_filter kewyord
  448. @skip_pyarrow
  449. @pytest.mark.parametrize(
  450. "na_filter,index_data", [(False, ["", "5"]), (True, [np.nan, 5.0])]
  451. )
  452. def test_no_na_filter_on_index(all_parsers, na_filter, index_data):
  453. # see gh-5239
  454. #
  455. # Don't parse NA-values in index unless na_filter=True
  456. parser = all_parsers
  457. data = "a,b,c\n1,,3\n4,5,6"
  458. expected = DataFrame({"a": [1, 4], "c": [3, 6]}, index=Index(index_data, name="b"))
  459. result = parser.read_csv(StringIO(data), index_col=[1], na_filter=na_filter)
  460. tm.assert_frame_equal(result, expected)
  461. def test_inf_na_values_with_int_index(all_parsers):
  462. # see gh-17128
  463. parser = all_parsers
  464. data = "idx,col1,col2\n1,3,4\n2,inf,-inf"
  465. # Don't fail with OverflowError with inf's and integer index column.
  466. out = parser.read_csv(StringIO(data), index_col=[0], na_values=["inf", "-inf"])
  467. expected = DataFrame(
  468. {"col1": [3, np.nan], "col2": [4, np.nan]}, index=Index([1, 2], name="idx")
  469. )
  470. tm.assert_frame_equal(out, expected)
  471. @skip_pyarrow
  472. @pytest.mark.parametrize("na_filter", [True, False])
  473. def test_na_values_with_dtype_str_and_na_filter(all_parsers, na_filter):
  474. # see gh-20377
  475. parser = all_parsers
  476. data = "a,b,c\n1,,3\n4,5,6"
  477. # na_filter=True --> missing value becomes NaN.
  478. # na_filter=False --> missing value remains empty string.
  479. empty = np.nan if na_filter else ""
  480. expected = DataFrame({"a": ["1", "4"], "b": [empty, "5"], "c": ["3", "6"]})
  481. result = parser.read_csv(StringIO(data), na_filter=na_filter, dtype=str)
  482. tm.assert_frame_equal(result, expected)
  483. @skip_pyarrow
  484. @pytest.mark.parametrize(
  485. "data, na_values",
  486. [
  487. ("false,1\n,1\ntrue", None),
  488. ("false,1\nnull,1\ntrue", None),
  489. ("false,1\nnan,1\ntrue", None),
  490. ("false,1\nfoo,1\ntrue", "foo"),
  491. ("false,1\nfoo,1\ntrue", ["foo"]),
  492. ("false,1\nfoo,1\ntrue", {"a": "foo"}),
  493. ],
  494. )
  495. def test_cast_NA_to_bool_raises_error(all_parsers, data, na_values):
  496. parser = all_parsers
  497. msg = (
  498. "(Bool column has NA values in column [0a])|"
  499. "(cannot safely convert passed user dtype of "
  500. "bool for object dtyped data in column 0)"
  501. )
  502. with pytest.raises(ValueError, match=msg):
  503. parser.read_csv(
  504. StringIO(data),
  505. header=None,
  506. names=["a", "b"],
  507. dtype={"a": "bool"},
  508. na_values=na_values,
  509. )
  510. @skip_pyarrow
  511. def test_str_nan_dropped(all_parsers):
  512. # see gh-21131
  513. parser = all_parsers
  514. data = """File: small.csv,,
  515. 10010010233,0123,654
  516. foo,,bar
  517. 01001000155,4530,898"""
  518. result = parser.read_csv(
  519. StringIO(data),
  520. header=None,
  521. names=["col1", "col2", "col3"],
  522. dtype={"col1": str, "col2": str, "col3": str},
  523. ).dropna()
  524. expected = DataFrame(
  525. {
  526. "col1": ["10010010233", "01001000155"],
  527. "col2": ["0123", "4530"],
  528. "col3": ["654", "898"],
  529. },
  530. index=[1, 3],
  531. )
  532. tm.assert_frame_equal(result, expected)
  533. @skip_pyarrow
  534. def test_nan_multi_index(all_parsers):
  535. # GH 42446
  536. parser = all_parsers
  537. data = "A,B,B\nX,Y,Z\n1,2,inf"
  538. result = parser.read_csv(
  539. StringIO(data), header=list(range(2)), na_values={("B", "Z"): "inf"}
  540. )
  541. expected = DataFrame(
  542. {
  543. ("A", "X"): [1],
  544. ("B", "Y"): [2],
  545. ("B", "Z"): [np.nan],
  546. }
  547. )
  548. tm.assert_frame_equal(result, expected)
  549. @xfail_pyarrow
  550. def test_bool_and_nan_to_bool(all_parsers):
  551. # GH#42808
  552. parser = all_parsers
  553. data = """0
  554. NaN
  555. True
  556. False
  557. """
  558. with pytest.raises(ValueError, match="NA values"):
  559. parser.read_csv(StringIO(data), dtype="bool")
  560. def test_bool_and_nan_to_int(all_parsers):
  561. # GH#42808
  562. parser = all_parsers
  563. data = """0
  564. NaN
  565. True
  566. False
  567. """
  568. with pytest.raises(ValueError, match="convert|NoneType"):
  569. parser.read_csv(StringIO(data), dtype="int")
  570. def test_bool_and_nan_to_float(all_parsers):
  571. # GH#42808
  572. parser = all_parsers
  573. data = """0
  574. NaN
  575. True
  576. False
  577. """
  578. result = parser.read_csv(StringIO(data), dtype="float")
  579. expected = DataFrame.from_dict({"0": [np.nan, 1.0, 0.0]})
  580. tm.assert_frame_equal(result, expected)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...