How to fix a ParserWarning error (aka "don't set both dtype and converter")

323 views
Skip to first unread message

Matt Anonyme

unread,
Dec 20, 2017, 9:30:15 AM12/20/17
to PyData
Hi,

When loading my csv via read_csv with this snippet (dtypes setting the "tcp.flags" column to an integer):


 dtype
=dtypes, converters={ "tcp.flags": lambda x: int(x, 16),

it would generate a warning

data.py:151: ParserWarning: Both a converter and dtype were specified for column tcp.flags - only the converter will be used

so I modified the code to

 dtype
=dtypes.pop("tcp.flags"), converters={ "tcp.flags": lambda x: int(x, 16),

and so now I get

 File "pandas/_libs/parsers.pyx", line 1189, in pandas._libs.parsers.TextReader._convert_tokens
TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'safe'




I understand the new error but then I wonder why only one of converter or dtype should be used as setting both makes sense in my scenario. How would you advise me to fix the code so as to have no warning/error ? I guess I can defer the "tcp.flags" column conversion from float to int later but it feels worse than the current way.


Thanks for the great library

Cheers
Reply all
Reply to author
Forward
0 new messages