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.

pd.read_table: Using space as delimiter on file with trailing space gives cryptic error

See original GitHub issue
import numpy as np
import os
import pandas as pd

# put test.csv in same folder as script
mydir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(mydir, "test.csv")

df = pd.read_table(csv_path, sep=' ',
                   comment='#',
                   header=None,
                   skip_blank_lines=True,
                   names=["A", "B", "C", "D", "E", "F", "G"],
                   dtype={"A": np.int32,
                       "B": np.int32,
                       "C": np.float64,
                       "D": np.float64,
                       "E": np.float64,
                       "F": np.float64,
                       "G": np.int32})

test.csv:

2270433 3 21322.889 11924.667 5228.753 1.0 -1 
2270432 3 21322.297 11924.667 5228.605 1.0 2270433 

Problem description

Attempting to load test.csv with pd.read_table() results in the following errors: TypeError: Cannot cast array from dtype('float64') to dtype('int32') according to the rule 'safe'

and

ValueError: cannot safely convert passed user dtype of int32 for float64 dtyped data in column 2

Expected behavior:

Either trailing whitespace is ignored by Pandas, or throw a more informative error than “cannot safely convert passed user dtype of int32 for float64”. It took me a really long time to figure out that this was caused by trailing spaces in the csv.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
simonjayhawkinscommented, Jun 11, 2021

removing milestone. but it looks like the PR is almost ready

1reaction
gfyoungcommented, Jul 8, 2018

@asishm : Thanks for pointing out. I think the docs could be clarified further to make this more explicit. That discrepancy that you found is a bug in the Python engine IMO. Malformed CSV shouldn’t just drop columns silently like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can pandas handle variable-length whitespace as column ...
I have a textfile where columns are separated by variable amounts of whitespace. Is it possible to load this file directly as a...
Read more >
pandas.read_table — pandas 1.5.2 documentation
Read general delimited file into DataFrame. ... By file-like object, we refer to objects with a read() method, ... Skip spaces after delimiter....
Read more >
Dealing with extra white spaces while reading CSV in Pandas
Photo by Kelly Sikkema on Unsplash. My colleague says that .csv is not an essay so we don't expect any blank spaces behind...
Read more >
Understanding Delimiters in Pandas read_csv() Function
csv file format has data items separated by a delimiter other than a comma. This includes semicolon, colon, tab space, vertical bars, etc....
Read more >
How to read a CSV file to a Dataframe with custom delimiter in ...
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. pandas package is one 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