question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

option dtype in pandas.read_csv does not work properly for mulilevel columns

See original GitHub issue
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas. 1.2.2


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd df= pd.DataFrame({ (‘A’, ‘int16’): pd.Series([1, 2, 3, 4], dtype=‘int16’), (‘A’, ‘int32’): pd.Series([132, 232, 332, 432], dtype=‘int32’), (‘B’, ‘float32’): pd.Series([1.01, 1.02, 1.03, 1.04], dtype=‘float32’), (‘B’, ‘int16’): pd.Series([21, 22, 23, 24], dtype=‘int16’)}) print(df) df.to_csv(‘test_df.csv’) print(df.dtypes)

full column name tuples with level 0/1 labels don’t work

df_new= pd.read_csv( ‘test_df.csv’, header=list(range(2)), dtype = { (‘A’, ‘int16’): ‘int16’, (‘A’, ‘int32’): ‘int32’ }) print(df_new.dtypes)

See my SO article for more detailed infos: https://stackoverflow.com/questions/54699527/dtype-is-ignored-when-using-multilevel-columns

Problem description

Although the data types where passed in read_csv, they are not applied. dtype in read_csv only seems to work for column names that contain only one level. For Multilevel columns it generally does not seem to work.

Expected Output

Unnamed: 0_level_0 Unnamed: 0_level_1 int64 A int16 int16 int32 int32 B float32 float64 int16 int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926db8f7541c356066dcadabf854487738de python : 3.7.9.final.0 python-bits : 64 OS : Linux OS-release : 5.4.0-77-generic Version : #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8

pandas : 1.2.2 numpy : 1.20.1 pytz : 2021.1 dateutil : 2.8.1 pip : 21.0.1 setuptools : 52.0.0.post20210125 Cython : 0.29.23 pytest : 6.2.2 hypothesis : None sphinx : 4.0.1 blosc : None feather : None xlsxwriter : None lxml.etree : 4.5.0 html5lib : 1.1 pymysql : None psycopg2 : 2.8.6 (dt dec pq3 ext lo64) jinja2 : 2.11.3 IPython : 7.22.0 pandas_datareader: None bs4 : 4.9.3 bottleneck : None fsspec : None fastparquet : None gcsfs : None matplotlib : 3.3.4 numexpr : 2.7.3 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 3.0.0 pyxlsb : None s3fs : None scipy : 1.6.2 sqlalchemy : None tables : 3.6.1 tabulate : None xarray : None xlrd : None xlwt : None numba : 0.51.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcomiecommented, Jul 10, 2021

Draft fix below. I’ll look for corner cases and applicable helper functions, write test code, then submit the PR.

diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx
index e5e61e409c..efa4678579 100644
--- a/pandas/_libs/parsers.pyx
+++ b/pandas/_libs/parsers.pyx
@@ -1281,7 +1281,10 @@ cdef class TextReader:
                 if j >= len(self.header[0]):
                     return j
                 else:
-                    return self.header[0][j]
+                    if len(self.header) == 1:
+                        return self.header[0][j]
+                    else:
+                        return tuple(header_row[j] for header_row in self.header)
             else:
                 return None
0reactions
jmcomiecommented, Jul 9, 2021

Candidate fix is an update to pandas/_libs/parsers.pyx:cdef class TextReader._get_column_name to make it multi-index aware. The result of this function is used to do the dtype lookup but at present, in the code lit by our test case, it returns only the first header row value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dtype is ignored when using multilevel columns - Stack Overflow
When using DataFrame.read_csv with multi level columns (read with header= ) pandas seems to ignore the dtype= keyword. Is there a way to...
Read more >
pandas.read_csv — pandas 1.5.2 documentation
Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}, optional....
Read more >
Manipulating DataFrames with Pandas - Trenton McKinney
You'll then assign a new DataFrame by selecting the list of columns ['winner', 'total', 'voters']. The CSV file is provided to you in...
Read more >
Pandas read_csv() tricks you should know to speed up your ...
Often, you'll work with data in CSV files and run into problems at the ... Dealing with columns; Parsing date columns; Setting data...
Read more >
Fun with Pandas Groupby, Aggregate, Multi-Index and Unstack
One nagging issue is that using mean() function on grouped dataframe has the same column names. Although now we have mean values of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found