Skip to content

ENH: Add defaultdict support for dtype in read_csv #46051

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

Merged
merged 9 commits into from
Mar 22, 2022
Merged

Conversation

phofl
Copy link
Member

@phofl phofl commented Feb 18, 2022

@@ -471,6 +471,9 @@ def _clean_mapping(self, mapping):
if isinstance(col, int) and col not in self.orig_names:
col = self.orig_names[col]
clean[col] = v
if isinstance(mapping, defaultdict):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also support defaultdicts directly instead of converting here. but this simplifies adding support for converters to and also we do not have to keep defaultdicts in mind every time we casses the dtypes

@mroeschke mroeschke added Enhancement IO CSV read_csv, to_csv labels Feb 22, 2022
@mroeschke mroeschke added this to the 1.5 milestone Feb 22, 2022
@jreback
Copy link
Contributor

jreback commented Feb 27, 2022

cool can you rebase

dtype["b.1"] = "int64"
parser = all_parsers
result = parser.read_csv(StringIO(data), dtype=dtype)
expected = DataFrame({"a": [1], "b": 2.0, "a.1": [3], "b.2": [4.0], "b.1": [5]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you miss the [] on "b", looks different but dont think there is a reason

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot it yes, but does not really make a difference.

Added nevertheless to be more consistent

@jreback
Copy link
Contributor

jreback commented Mar 16, 2022

this looked fine, can you merge master @phofl

@phofl
Copy link
Member Author

phofl commented Mar 22, 2022

Done

@jreback jreback merged commit 40fbd59 into pandas-dev:main Mar 22, 2022
@jreback
Copy link
Contributor

jreback commented Mar 22, 2022

thanks @phofl very nice as always!

@phofl phofl deleted the 41574 branch June 6, 2022 20:52
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO CSV read_csv, to_csv
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: support defaultdict in read_csv dtype parameter
4 participants