Skip to content

Commit 302ede3

Browse files
code sample for pandas-dev#46812
1 parent 33ef3d7 commit 302ede3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

bisect/46812.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# BUG: Error writing DataFrame with categorical type column and "Int" data to a CSV file ("int" works of course) #46812
2+
3+
import numpy as np
4+
import pandas as pd
5+
6+
print(pd.__version__)
7+
8+
d = {
9+
"name": ["bob", "todd", "sarah", "john"],
10+
"gp": [1, 2, np.NaN, 2],
11+
"score": [90, 40, 80, 98],
12+
}
13+
df = pd.DataFrame(d)
14+
df.name = df.name.astype("category")
15+
df.gp = df.gp.astype("Int16")
16+
df.gp = df.gp.astype("category")
17+
18+
result = df.to_csv()
19+
print(result)
20+
21+
expected = ",name,gp,score\n0,bob,1,90\n1,todd,2,40\n2,sarah,,80\n3,john,2,98\n"
22+
assert result == expected

0 commit comments

Comments
 (0)