-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: to_json fails writing to GCS with compression #39985
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
Comments
@dariobig thank you for the report! I assume the error is something like "str is expected but bytes is provided", is that correct? Does it work with pandas <1.2? |
Not sure, this is new code I'll give it a try. |
@twoertwein I've retried with pandas 1.1.5 and instead of failing I get an empty file whether I use compression or not: These are the package versions used:
|
thank for testing! I think there might be two possible ways to address this 1) look into why we need "wt" for |
For what it's worth I can confirm dariobig's findings. This works with Pandas 1.2.1. The regression appears to be with change #39440. My findings (a lot repeats with dariobig) - Reproduction: from io import BytesIO
import pandas as pd
dataframe = pd.DataFrame([1, 2, 3], columns=['a'])
object_stream = BytesIO()
dataframe.to_json(object_stream, compression='gzip', orient='records', lines=True) Result: if path_or_buf is not None:
# apply compression and byte/text conversion
with get_handle(
path_or_buf, "wt", compression=compression, storage_options=storage_options
) as handles:
> handles.handle.write(s)
E TypeError: a bytes-like object is required, not 'str'
../../.env/lib/python3.7/site-packages/pandas/io/json/_json.py:105: TypeError From what I can tell, _json's get_handle uses a static 'wt' mode which now, with the latest changes to common's _is_binary_mode, will always return false rather than check the path_or_buf instance type, thereby omitting the b flag on the mode passed to the rest of get_handle. |
thank you for your investigation! I think we do not need |
yes, the tests In my code once again pass when I manually change to pandas 1.2.2 to just 'w'. |
Works for me too! 🍾
I don't know about before. As I said 1.1.5 doesn't work at all for me (empty file). |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
Error writing compressed stream using gcs. Removing compression works fine.
Expected Output
No error
Output of
pd.show_versions()
pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : 0.7.2
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: