-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
BUG: read_fwf modifies / corrupts object (string) whitespace data #51569
Comments
Consider the following code:
The one dataframe creation method where I explicitly specify the column widths is the only method that changes my data when reading it. |
Currently, there is a bit of a work-around for this issue, but it's quite an "anti-pattern", and very counter-intuitive. Adding a Using the code snippet from previous comment, but adding a
The source of this behaviour is found at: pandas/pandas/io/parsers/python_parser.py Line 1182 in 3f3102b
pandas/pandas/io/parsers/python_parser.py Line 1281 in 3f3102b
It seems that A |
Final note (for now): There is no mention of delimiters for There is a confusing mention of them in the user_guide at
It can be argued that trailing spaces are "filler characters", but not leading Like in a Python script file, they have significance and should not be removed |
take |
…lowing more control over handling of whitespace in fields and removing the requirement to specify a `delimiter` in order to preserve whitespace. (pandas-dev#51569) Signed-off-by: Ronald Barnes <ron@ronaldbarnes.ca>
…andas-dev#51569) Signed-off-by: Ronald Barnes <ron@ronaldbarnes.ca>
…arguments for `read_fwf`. (pandas-dev#51569) Signed-off-by: Ronald Barnes <ron@ronaldbarnes.ca>
I am using read_fwf function for data processing and I ran into a different behavior where: Without specifying delimiter or lineterminator keywords -- the function works if file has more than 1 line (except it fails to read last line in a file), but fails when only 1 line in the text file (by default file has no header row): Only when I specify delimiter AND lineterminator -- does the function work properly to read all lines in the file: Seems to me that the "required TextReader kwargs" for read_fwf should either be documented (easy path to avoid regression) or the defaults need to re-visited for the purpose of processing a "fixed width file" (by definition no delimiters, typically no headers and many times, conditional column count based on a prefix value on the line, etc). |
Re-opening. Still hoping to hear feedback on current behaviour & effectiveness of this patch in addressing it. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Note in code sample that the Credit_score has had leading (and trailing) spaces removed.
This is now irretrievably corrupted data.
Expected Behavior
Expected: The leading (at minimum) spaces are preserved, as they have significance.
The field width should match the
widths
orcolspecs
specified unless explicitly requesting otherwise.Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-32-generic
Version : #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 30 17:03:34 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_CA.UTF-8
LOCALE : en_CA.UTF-8
pandas : 1.5.3
numpy : 1.23.5
pytz : 2022.1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : 0.29.33
pytest : 7.2.1
hypothesis : 6.65.1
sphinx : 6.1.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.8.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : 0.56.4
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: