From 2122452a422925b566194e192bf9dc799fb1a5a4 Mon Sep 17 00:00:00 2001 From: blalterman Date: Thu, 5 Nov 2026 16:05:58 -0500 Subject: [PATCH 1/2] test(core): pin Ion sorting out-of-order rows with one warning Target tests, xfail(strict=True) until Ion sorts its data: Plasma, Spacecraft, Vector and Tensor sort rows out of time order with one warning that counts and locates them, while an Ion built directly keeps them and logs the vague 'not monotonically increasing' warning. Refs #507. Co-Authored-By: Claude Opus 5.5 --- tests/core/test_core_verify_datetimeindex.py | 48 ++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/tests/core/test_core_verify_datetimeindex.py b/tests/core/test_core_verify_datetimeindex.py index 860184acb..b4d351088 100644 --- a/tests/core/test_core_verify_datetimeindex.py +++ b/tests/core/test_core_verify_datetimeindex.py @@ -1,7 +1,9 @@ """Index checks every data-backed object runs when its data is set. ``Ion`` stands in for every ``Base`` subclass: setting data logs a warning for -an index that is not a ``DatetimeIndex`` and for one that is not increasing. +an index that is not a ``DatetimeIndex``, and sorts rows out of time order with +one warning that counts and locates them, as ``Plasma``, ``Spacecraft``, +``Vector`` and ``Tensor`` do. """ import logging @@ -39,15 +41,26 @@ def _ion_data(index): "index, expected", [ (pd.RangeIndex(2), {NOT_DATETIME}), - (pd.to_datetime(["2020-01-02", "2020-01-01"]), {NOT_INCREASING}), + pytest.param( + pd.to_datetime(["2020-01-02", "2020-01-01"]), + set(), + marks=pytest.mark.xfail( + strict=True, + raises=AssertionError, + reason="Ion keeps out-of-order rows and logs the vague 'not " + "monotonically increasing' warning; remove this marker when Ion " + "sorts its data", + ), + ), (pd.to_datetime(["2020-01-01", "2020-01-02"]), set()), ], ids=["range-index", "decreasing-datetimes", "increasing-datetimes"], ) def test_ion_warns_about_exactly_the_index_problems_present(caplog, index, expected): - """A non-datetime or non-increasing index is logged; a clean one is not. + """A non-datetime index is logged; a clean or merely out-of-order one is not. - The inputs are chosen so each case has exactly the listed problems. + The inputs are chosen so each case has exactly the listed problems. Rows + out of order are sorted with their own warning, not this module's two. ON FAILURE: the code is wrong. """ @@ -55,3 +68,30 @@ def test_ion_warns_about_exactly_the_index_problems_present(caplog, index, expec Ion(_ion_data(index), "p1") found = {msg for msg in (NOT_DATETIME, NOT_INCREASING) if msg in caplog.text} assert found == expected + + +@pytest.mark.xfail( + strict=True, + raises=AssertionError, + reason="Ion keeps out-of-order rows and logs the vague 'not monotonically " + "increasing' warning; expected one 'earlier than the row before them' " + "warning and sorted rows; remove this marker when Ion sorts its data", +) +def test_ion_sorts_out_of_order_rows_with_one_warning(caplog): + """An Ion built directly from rows out of time order sorts them, warning once. + + The rows given in time order 2, 0, 1 come back in time order, with the + values that came with each time, and the one warning counts them. + + ON FAILURE: the code is wrong. + """ + times = pd.to_datetime(["2020-01-03", "2020-01-01", "2020-01-02"]) + data = _ion_data(times) + data[("n", "")] = [3.0, 1.0, 2.0] + with caplog.at_level(logging.WARNING): + ion = Ion(data, "p1") + warnings = [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING] + assert len(warnings) == 1 + assert "1 of 3 rows are earlier than the row before them" in warnings[0] + assert ion.data.index.equals(times.sort_values()) + assert ion.data[("n", "")].tolist() == [1.0, 2.0, 3.0] From 8e9eae37c232207940b4419496ef04b61f2af6f0 Mon Sep 17 00:00:00 2001 From: blalterman Date: Thu, 5 Nov 2026 16:12:08 -0500 Subject: [PATCH 2/2] fix(core): sort an Ion's out-of-order rows as its siblings do Ion.set_data now puts its data in time order through _put_in_time_order_unless_checked, as Vector and Tensor do and as Plasma and Spacecraft do through _put_in_time_order: one warning counts and locates the rows out of order, and the rows are sorted. Before, an Ion built directly kept them and Base's _verify_datetimeindex logged only 'not monotonically increasing'. That warning is now reached only by an index whose values cannot be compared. An Ion built by a Plasma inherits checked times and is unchanged. Before: both target tests xfailed. With the fix and markers in place: both XPASS(strict). Markers removed: full suite 6649 passed. Closes #507. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ src/solarwindpy/core/base.py | 12 ++++++------ src/solarwindpy/core/ions.py | 8 ++++++-- tests/core/test_core_verify_datetimeindex.py | 19 +------------------ 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c5aaa344..7b9aba25a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 every spelling of the colour (`color`, `c`, `colors`, `edgecolor`, `edgecolors`, `ec`) now sets the segments' colour; before, only `color` did, `c` raised `AttributeError`, and the others were overridden by the points' colour. Giving two of them raises `TypeError`. +- `Ion` built directly from rows out of time order now sorts them with one warning that counts + and locates them, as `Plasma`, `Spacecraft`, `Vector` and `Tensor` do. Before, it kept the rows + unsorted and logged only that the index was "not monotonically increasing". An `Ion` built by a + `Plasma` was already sorted and is unchanged. - Release automation: the conda-forge tracking issue now reads the sdist's SHA256 from PyPI's JSON API, waiting up to ten minutes with backoff while a new release is not yet listed, instead of downloading the file and failing on the CDN's early 404. Its body fills in the version, diff --git a/src/solarwindpy/core/base.py b/src/solarwindpy/core/base.py index 4bf8d2da9..c6906c28b 100644 --- a/src/solarwindpy/core/base.py +++ b/src/solarwindpy/core/base.py @@ -456,12 +456,11 @@ def _verify_datetimeindex(self, data: pd.DataFrame) -> None: Completeness and order are decided by :meth:`_time_disorder`, as for :meth:`_put_in_time_order`. - :class:`~solarwindpy.core.plasma.Plasma` and - :class:`~solarwindpy.core.spacecraft.Spacecraft` sort their data - before this runs, so they never reach the order warning. An index whose - values cannot be compared, such as ``[1, "a", 2.0]``, has no order: it - gets the order warning, as pandas' ``is_monotonic_increasing`` is False - for it, and the data are kept. + Every subclass of :class:`Base` sorts its data before this runs, with + one warning that counts and locates the rows out of order, so only an + index whose values cannot be compared, such as ``[1, "a", 2.0]``, + reaches the order warning: it has no order, as pandas' + ``is_monotonic_increasing`` is False for it, and the data are kept. Parameters ---------- @@ -580,6 +579,7 @@ def _put_in_time_order_unless_checked(self, new: pd.DataFrame) -> pd.DataFrame: r"""Return ``new`` in time order, warning once, unless already checked. For classes that sort their data on setting, as + :class:`~solarwindpy.core.ions.Ion`, :class:`~solarwindpy.core.vector.Vector` and :class:`~solarwindpy.core.tensor.Tensor` do. Missing timestamps (``NaT``) raise :class:`ValueError`; see :meth:`_put_in_time_order`. diff --git a/src/solarwindpy/core/ions.py b/src/solarwindpy/core/ions.py index 2bd7f59b3..acee2cb6f 100644 --- a/src/solarwindpy/core/ions.py +++ b/src/solarwindpy/core/ions.py @@ -131,13 +131,17 @@ def set_data(self, data: pd.DataFrame) -> None: Parameters ---------- data : pd.DataFrame - The data to set for the ion. + The data to set for the ion. Rows out of time order are sorted + with one warning, as :class:`~solarwindpy.core.plasma.Plasma` + sorts its data. Raises ------ ValueError - If the data column names are unrecognized. + If the data column names are unrecognized, or the time index holds + missing timestamps (``NaT``). """ + data = self._put_in_time_order_unless_checked(data) super().set_data(data) if data.columns.names == ["M", "C", "S"]: diff --git a/tests/core/test_core_verify_datetimeindex.py b/tests/core/test_core_verify_datetimeindex.py index b4d351088..4f93a26b5 100644 --- a/tests/core/test_core_verify_datetimeindex.py +++ b/tests/core/test_core_verify_datetimeindex.py @@ -41,17 +41,7 @@ def _ion_data(index): "index, expected", [ (pd.RangeIndex(2), {NOT_DATETIME}), - pytest.param( - pd.to_datetime(["2020-01-02", "2020-01-01"]), - set(), - marks=pytest.mark.xfail( - strict=True, - raises=AssertionError, - reason="Ion keeps out-of-order rows and logs the vague 'not " - "monotonically increasing' warning; remove this marker when Ion " - "sorts its data", - ), - ), + (pd.to_datetime(["2020-01-02", "2020-01-01"]), set()), (pd.to_datetime(["2020-01-01", "2020-01-02"]), set()), ], ids=["range-index", "decreasing-datetimes", "increasing-datetimes"], @@ -70,13 +60,6 @@ def test_ion_warns_about_exactly_the_index_problems_present(caplog, index, expec assert found == expected -@pytest.mark.xfail( - strict=True, - raises=AssertionError, - reason="Ion keeps out-of-order rows and logs the vague 'not monotonically " - "increasing' warning; expected one 'earlier than the row before them' " - "warning and sorted rows; remove this marker when Ion sorts its data", -) def test_ion_sorts_out_of_order_rows_with_one_warning(caplog): """An Ion built directly from rows out of time order sorts them, warning once.