Skip to content

Commit 50b29e7

Browse files
committed
Fixing pytest cases per request from mroeschke. Switching whatsnew version per request from mroeschke.
1 parent 6d1499a commit 50b29e7

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ Other enhancements
333333
- :meth:`DataFrame.set_index` now supports a ``copy`` keyword. If ``False``, the underlying data is not copied when a new :class:`DataFrame` is returned (:issue:`48043`)
334334
- The method :meth:`.ExtensionArray.factorize` accepts ``use_na_sentinel=False`` for determining how null values are to be treated (:issue:`46601`)
335335
- The ``Dockerfile`` now installs a dedicated ``pandas-dev`` virtual environment for pandas development instead of using the ``base`` environment (:issue:`48427`)
336-
- :meth:`DataFrame.to_json` now supports a ``mode`` keyword with supported inputs 'w' and 'a'. Defaulting to 'w', 'a' can be used when lines=True and orient='records' to append record oriented json lines to an existing json file. (:issue:`35849`)
337336

338337
.. ---------------------------------------------------------------------------
339338
.. _whatsnew_150.notable_bug_fixes:

doc/source/whatsnew/v1.6.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Other enhancements
3131
- :meth:`.GroupBy.quantile` now preserving nullable dtypes instead of casting to numpy dtypes (:issue:`37493`)
3232
- :meth:`Series.add_suffix`, :meth:`DataFrame.add_suffix`, :meth:`Series.add_prefix` and :meth:`DataFrame.add_prefix` support an ``axis`` argument. If ``axis`` is set, the default behaviour of which axis to consider can be overwritten (:issue:`47819`)
3333
- :func:`assert_frame_equal` now shows the first element where the DataFrames differ, analogously to ``pytest``'s output (:issue:`47910`)
34-
-
34+
- :meth:`DataFrame.to_json` now supports a ``mode`` keyword with supported inputs 'w' and 'a'. Defaulting to 'w', 'a' can be used when lines=True and orient='records' to append record oriented json lines to an existing json file. (:issue:`35849`)
3535

3636
.. ---------------------------------------------------------------------------
3737
.. _whatsnew_160.notable_bug_fixes:

pandas/tests/io/json/test_readlines.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,20 @@ def test_to_json_append_mode(mode_):
330330
# Test ValueError when mode is not supported option
331331
df = DataFrame({"col1": [1, 2], "col2": ["a", "b"]})
332332
msg = (
333-
f"mode={repr(mode_)} is not a valid option."
333+
f"mode={mode_} is not a valid option."
334334
"Only 'w' and 'a' are currently supported."
335335
)
336336
with pytest.raises(ValueError, match=msg):
337337
df.to_json(mode=mode_, lines=False, orient="records")
338338

339339

340-
def to_json_append_output():
340+
def to_json_append_output_consistent_columns():
341341
# GH 35849
342-
# Testing that resulting outputs read in as expected.
342+
# Testing that resulting output reads in as expected.
343+
# Testing same columns, new rows
343344
df1 = DataFrame({"col1": [1, 2], "col2": ["a", "b"]})
344345
df2 = DataFrame({"col1": [3, 4], "col2": ["c", "d"]})
345-
df3 = DataFrame({"col2": ["e", "f"], "col3": ["!", "#"]})
346-
df4 = DataFrame({"col4": [True, False]})
347346

348-
# Test 1, df1 and df2
349347
expected = DataFrame({"col1": [1, 2, 3, 4], "col2": ["a", "b", "c", "d"]})
350348
with tm.ensure_clean("test.json") as path:
351349
# Save dataframes to the same file
@@ -356,7 +354,46 @@ def to_json_append_output():
356354
result = read_json(path, lines=True)
357355
tm.assert_frame_equal(result, expected)
358356

357+
358+
def to_json_append_output_inconsistent_columns():
359+
# GH 35849
360+
# Testing that resulting output reads in as expected.
361+
# Testing one new column, one old column, new rows
362+
df1 = DataFrame({"col1": [1, 2], "col2": ["a", "b"]})
363+
df3 = DataFrame({"col2": ["e", "f"], "col3": ["!", "#"]})
364+
359365
# Test 2: df1, df2, df3, df4 (in that order)
366+
expected = DataFrame(
367+
{
368+
"col1": [
369+
1,
370+
2,
371+
None,
372+
None,
373+
],
374+
"col2": ["a", "b", "e", "f"],
375+
"col3": [None, None, "!", "#"],
376+
}
377+
)
378+
with tm.ensure_clean("test.json") as path:
379+
# Save dataframes to the same file
380+
df1.to_json(path, mode="a", lines=True, orient="records")
381+
df3.to_json(path, mode="a", lines=True, orient="records")
382+
383+
# Read path file
384+
result = read_json(path, lines=True)
385+
tm.assert_frame_equal(result, expected)
386+
387+
388+
def to_json_append_output_different_columns():
389+
# GH 35849
390+
# Testing that resulting output reads in as expected.
391+
# Testing same, differing and new columns
392+
df1 = DataFrame({"col1": [1, 2], "col2": ["a", "b"]})
393+
df2 = DataFrame({"col1": [3, 4], "col2": ["c", "d"]})
394+
df3 = DataFrame({"col2": ["e", "f"], "col3": ["!", "#"]})
395+
df4 = DataFrame({"col4": [True, False]})
396+
360397
expected = DataFrame(
361398
{
362399
"col1": [1, 2, 3, 4, None, None, None, None],
@@ -376,7 +413,17 @@ def to_json_append_output():
376413
result = read_json(path, lines=True)
377414
tm.assert_frame_equal(result, expected)
378415

379-
# Test 3: df4, df3, df2, df1 (in that order)
416+
417+
def to_json_append_output_different_columns_reordered():
418+
# GH 35849
419+
# Testing that resulting output reads in as expected.
420+
# Testing specific result column order.
421+
df1 = DataFrame({"col1": [1, 2], "col2": ["a", "b"]})
422+
df2 = DataFrame({"col1": [3, 4], "col2": ["c", "d"]})
423+
df3 = DataFrame({"col2": ["e", "f"], "col3": ["!", "#"]})
424+
df4 = DataFrame({"col4": [True, False]})
425+
426+
# df4, df3, df2, df1 (in that order)
380427
expected = DataFrame(
381428
{
382429
"col4": [True, False, None, None, None, None, None, None],

0 commit comments

Comments
 (0)