Skip to content

BUG:Characters like '/' are being replaced by '_' when importing a CSV file into Python with pandas. #58257

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

Closed
2 of 3 tasks
harishvspeaker opened this issue Apr 14, 2024 · 6 comments
Labels
Bug IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue

Comments

@harishvspeaker
Copy link

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

import pandas as pd 
data =pd.reas_csv(r"csvfilepath.csv")

Issue Description

say i have a string row "he/him" when loaded into python it automatically gets converted into "he_him" and when the same data frame is saved back into a csv format "_" is retained

Expected Behavior

/...

Installed Versions

Replace this line with the output of pd.show_versions()

@harishvspeaker harishvspeaker added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 14, 2024
@asishm
Copy link
Contributor

asishm commented Apr 14, 2024

Can you provide a minimal bug report ?

@eshaready
Copy link
Contributor

take

@rhshadrach rhshadrach added IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 15, 2024
@eshaready
Copy link
Contributor

Hey! I'm not able to reproduce this issue - I'm able to use read_csv and to_csv with no issues with / characters in columns. Follow up if the issue persists, or maybe provide the specific file that caused the error!

@aabhinavg
Copy link

Hi @harishvspeaker,

I've tried reproducing the bug using the test case you described, but the code appears to work as expected. Here's the test case I used:

import pandas as pd
import io

def test_slash_character_replacement():
    # Create a sample CSV content with a string containing '/'
    csv_content = """column1,column2
    value1,he/him
    value2,she/her"""

    # Use io.StringIO to simulate reading from a file
    csv_file = io.StringIO(csv_content)

    # Read the CSV file using pandas read_csv
    df = pd.read_csv(csv_file)

    # Check if the '/' character is retained in the DataFrame
    assert df.loc[0, 'column2'] == 'he/him', "Expected '/' character in the data"
    assert df.loc[1, 'column2'] == 'she/her', "Expected '/' character in the data"

    # Optionally, save the DataFrame back to CSV and check the content
    # with io.StringIO to avoid file I/O
    output_csv = io.StringIO()
    df.to_csv(output_csv, index=False)
    output_csv.seek(0)
    saved_csv_content = output_csv.read()
    print(saved_csv_content)

    # Verify that the saved CSV content retains the original '/' character
    assert 'he/him' in saved_csv_content, "Expected '/' character in saved CSV content"
    assert 'she/her' in saved_csv_content, "Expected '/' character in saved CSV content"

# Run the test function
test_slash_character_replacement()

Output

column1,column2
value1,he/him
value2,she/her

The '/' character is retained in both the DataFrame and the saved CSV content.

Could you please provide a specific test case or CSV file where you are seeing the bug? This would help us understand the issue better and work on a fix.

Thank you!

@eshaready eshaready removed their assignment Apr 16, 2024
@aabhinavg
Copy link

Hi @harishvspeaker I have been working on investigating the bug you described, but unfortunately, I have not been able to reproduce the problem.

Could you please share the test case and more detailed steps on how we can produce the bug? Specific information such as data inputs, environment details (e.g., OS version, browser, etc.), and any other pertinent information will be helpful for me or the open source team to better understand and resolve the issue.

If you are unable to provide additional details or if the issue is no longer relevant, please close the issue.

@mroeschke
Copy link
Member

Closing as it seems like there's no reproducible example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

6 participants