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 860184acb..4f93a26b5 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,16 @@ def _ion_data(index): "index, expected", [ (pd.RangeIndex(2), {NOT_DATETIME}), - (pd.to_datetime(["2020-01-02", "2020-01-01"]), {NOT_INCREASING}), + (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"], ) 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 +58,23 @@ 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 + + +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]