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.

Do not manually loop over all rows when encoding a dataframe as JSON

See original GitHub issue

See: https://github.com/pydata/pandas-gbq/pull/25#discussion_r155355165

Currently the code loops over each row to encode it as JSON. This could be sped up by calling to_json() on the whole dataframe instead.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tswastcommented, Feb 10, 2018

Looks like something like this should work to avoid having to write to a temporary file.

def encode_chunk(df):
    """Return a file-like object of CSV-encoded rows.

    Args:
      df (pandas.DataFrame): A chunk of a dataframe to encode
    """
    csv_buffer = six.StringIO()
    df.to_csv(
        csv_buffer, index=False, header=False, encoding='utf-8',
        date_format='%Y-%m-%d %H:%M')

    # Convert to a BytesIO buffer so that unicode text is properly handled.
    # See: https://github.com/pydata/pandas-gbq/issues/106
    body = csv_buffer.getvalue()
    if isinstance(body, bytes):
        body = body.decode('utf-8')
    body = body.encode('utf-8')
    return six.BytesIO(body)
1reaction
max-sixtycommented, Feb 10, 2018

CSV also smaller! And no nesting possible so no loss there

But if json is preferred, I think you can use orient=records

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do not manually loop over all rows when encoding a ... - GitHub
Do not manually loop over all rows when encoding a dataframe as JSON #96 ... Currently the code loops over each row to...
Read more >
placing try-except with for loop to avoid errors reading text file
Goal: I want to essentially skip any lines in the JSON file that are problematic, but add any I can to my pandas...
Read more >
How to convert pandas DataFrame into JSON in Python?
Convert pandas DataFrame into JSON ; path_or_buf, string or filename, optional, File path or object. If not specified, the result is returned as ......
Read more >
pandas.DataFrame.to_json — pandas 1.5.2 documentation
Encoding /decoding a Dataframe using 'records' formatted JSON. Note that index labels are not preserved with this encoding.
Read more >
Pandas - Convert DataFrame to JSON String
You can convert pandas DataFrame to JSON string by using DataFrame.to_json() method. This method takes a very important param orient which ...
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