-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
PARTIAL FIX: Improve leading zeros preservation with dtype=str for dict-based dtypes #62242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
b39ff48
f8c4a23
6b80f87
e2916d0
1413d48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
) | ||
from pandas.core.dtypes.inference import is_integer | ||
|
||
from pandas.core.arrays.arrow.array import to_pyarrow_type | ||
|
||
from pandas.io._util import arrow_table_to_pandas | ||
from pandas.io.parsers.base_parser import ParserBase | ||
|
||
|
@@ -139,6 +141,30 @@ def handle_warning(invalid_row) -> str: | |
f"f{n}" for n in self.convert_options["include_columns"] | ||
] | ||
|
||
if self.dtype is not None: | ||
if isinstance(self.dtype, dict): | ||
column_types = {} | ||
for col, col_dtype in self.dtype.items(): | ||
source_dtype = pandas_dtype(col_dtype) | ||
|
||
try: | ||
target_dtype = to_pyarrow_type(source_dtype.type) | ||
if target_dtype: | ||
column_types[col] = target_dtype | ||
|
||
except TypeError: | ||
# TODO: Unsupported dtypes silently ignored - may cause | ||
# unexpected behavior when pyarrow applies default inference | ||
# instead of user's dtype | ||
pass | ||
|
||
if column_types: | ||
self.convert_options["column_types"] = column_types | ||
else: | ||
# TODO: Global dtypes not supported - may cause inconsistent behavior | ||
# between engines, especially for leading zero preservation | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if they pass a singleton, can we do something like
?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good thought, but it doesn't work. One of the first things I tried actually :) I documented a larger analysis on pyarrow here: apache/arrow#47502 You can see the relevant portion of pyarrow code here. Everything is mapped back to C++, and if the column name is not found, it uses the default (inferred) option. |
||
|
||
self.read_options = { | ||
"autogenerate_column_names": self.header is None, | ||
"skip_rows": self.header | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats an example where this happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I seem to remember it failed some test. I can look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the try/except block to test the theory, but I'm getting some failures. Not sure if it's the test suite or the change itself. I was getting some recent failures in the test suite anyway... they just don't seem related.
If the test suite will pass, I'm fine leaving it out. I think there was some historical reason for including it, during some of my earlier attempts at making this work.