Skip to content

Commit 86107d4

Browse files
FIX-#7624: Add proper implementation for Series.to_json (#7673)
Resovles #7624 <!-- Thank you for your contribution! Please review the contributing docs: https://modin.readthedocs.io/en/latest/development/contributing.html if you have questions about contributing. --> ## What do these changes do? <!-- Please give a short brief about these changes. --> - [x] first commit message and PR title follow format outlined [here](https://modin.readthedocs.io/en/latest/development/contributing.html#commit-message-formatting) > **_NOTE:_** If you edit the PR title to match this format, you need to add another commit (even if it's empty) or amend your last commit for the CI job that checks the PR title to pick up the new PR title. - [ ] passes `flake8 modin/ asv_bench/benchmarks scripts/doc_checker.py` - [ ] passes `black --check modin/ asv_bench/benchmarks scripts/doc_checker.py` - [ ] signed commit with `git commit -s` <!-- you can amend your commit with a signature via `git commit -amend -s` --> - [ ] Resolves #? <!-- issue must be created for each patch --> - [ ] tests added and passing - [ ] module layout described at `docs/development/architecture.rst` is up-to-date <!-- if you have added, renamed or removed files or directories please update the documentation accordingly --> --------- Signed-off-by: Devin Petersohn <devin.petersohn@snowflake.com>
1 parent 515d556 commit 86107d4

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

modin/core/execution/dispatching/factories/dispatcher.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ def to_csv(cls, *args, **kwargs):
400400
def to_json(cls, *args, **kwargs):
401401
return cls.get_factory()._to_json(*args, **kwargs)
402402

403+
@classmethod
404+
@_inherit_docstrings(factories.BaseFactory._to_json)
405+
def to_json_series(cls, *args, **kwargs):
406+
return cls.get_factory()._to_json_series(*args, **kwargs)
407+
403408
@classmethod
404409
@_inherit_docstrings(factories.BaseFactory._to_xml)
405410
def to_xml(cls, *args, **kwargs):

modin/core/execution/dispatching/factories/factories.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,20 @@ def _to_json(cls, *args, **kwargs):
473473
"""
474474
return cls.io_cls.to_json(*args, **kwargs)
475475

476+
@classmethod
477+
def _to_json_series(cls, *args, **kwargs):
478+
"""
479+
Write query compiler content of a Series to a JSON file.
480+
481+
Parameters
482+
----------
483+
*args : args
484+
Arguments to pass to the writer method.
485+
**kwargs : kwargs
486+
Arguments to pass to the writer method.
487+
"""
488+
return cls.io_cls.to_json_series(*args, **kwargs)
489+
476490
@classmethod
477491
def _to_xml(cls, *args, **kwargs):
478492
"""

modin/core/io/io.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,20 @@ def to_json(cls, obj, path, **kwargs): # noqa: PR01
757757

758758
return obj.to_json(path, **kwargs)
759759

760+
@classmethod
761+
@_inherit_docstrings(pandas.Series.to_json, apilink="pandas.Series.to_json")
762+
def to_json_series(cls, obj, path, **kwargs): # noqa: PR01
763+
"""
764+
Convert the object to a JSON string.
765+
766+
For parameters description please refer to pandas API.
767+
"""
768+
cls._maybe_warn_on_default(message="`to_json`")
769+
if isinstance(obj, BaseQueryCompiler):
770+
obj = obj.to_pandas().squeeze(axis=1)
771+
772+
return obj.to_json(path, **kwargs)
773+
760774
@classmethod
761775
@_inherit_docstrings(pandas.DataFrame.to_xml, apilink="pandas.DataFrame.to_xml")
762776
def to_xml(cls, obj, path_or_buffer, **kwargs): # noqa: PR01

modin/pandas/series.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@
2222
import numpy as np
2323
import pandas
2424
from pandas._libs import lib
25-
from pandas._typing import ArrayLike, Axis, DtypeObj, IndexKeyFunc, Scalar, Sequence
25+
from pandas._typing import (
26+
ArrayLike,
27+
Axis,
28+
DtypeObj,
29+
IndexKeyFunc,
30+
Scalar,
31+
Sequence,
32+
StorageOptions,
33+
)
2634
from pandas.api.types import is_integer
2735
from pandas.core.arrays import ExtensionArray
2836
from pandas.core.common import apply_if_callable, is_bool_indexer
@@ -2189,6 +2197,43 @@ def to_frame(
21892197

21902198
return DataFrame(self_cp)
21912199

2200+
def to_json(
2201+
self,
2202+
path_or_buf=None,
2203+
orient=None,
2204+
date_format=None,
2205+
double_precision=10,
2206+
force_ascii=True,
2207+
date_unit="ms",
2208+
default_handler=None,
2209+
lines=False,
2210+
compression="infer",
2211+
index=None,
2212+
indent=None,
2213+
storage_options: StorageOptions = None,
2214+
mode="w",
2215+
) -> str | None:
2216+
from modin.core.execution.dispatching.factories.dispatcher import (
2217+
FactoryDispatcher,
2218+
)
2219+
2220+
return FactoryDispatcher.to_json_series(
2221+
self._query_compiler,
2222+
path_or_buf,
2223+
orient=orient,
2224+
date_format=date_format,
2225+
double_precision=double_precision,
2226+
force_ascii=force_ascii,
2227+
date_unit=date_unit,
2228+
default_handler=default_handler,
2229+
lines=lines,
2230+
compression=compression,
2231+
index=index,
2232+
indent=indent,
2233+
storage_options=storage_options,
2234+
mode=mode,
2235+
)
2236+
21922237
def to_list(self) -> list: # noqa: RT01, D200
21932238
"""
21942239
Return a list of the values.

modin/tests/pandas/test_series.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ def test_to_list(data):
263263
assert np.array_equal(pd_res, md_res, equal_nan=True)
264264

265265

266+
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
267+
def test_to_json(data):
268+
modin_series, pandas_series = create_test_series(data)
269+
pd_res = pandas_series.to_json()
270+
md_res = modin_series.to_json()
271+
assert type(pd_res) is type(md_res)
272+
assert pd_res == md_res
273+
274+
266275
def test_accessing_index_element_as_property():
267276
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
268277
assert s.b == 20

0 commit comments

Comments
 (0)