Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/solarwindpy/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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`.
Expand Down
8 changes: 6 additions & 2 deletions src/solarwindpy/core/ions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
31 changes: 27 additions & 4 deletions tests/core/test_core_verify_datetimeindex.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,19 +41,40 @@ 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.
"""
with caplog.at_level(logging.WARNING):
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]
Loading