From 18e72a2e69e355b4879f1f081eb9d3302b279444 Mon Sep 17 00:00:00 2001 From: Vaibhav Srivastava Date: Sat, 22 Aug 2026 10:21:21 +0530 Subject: [PATCH] MultipleSeries.pca(): raise on unequal lengths instead of returning None pca() printed a plain string and fell off the end of the function when the series had different lengths, so callers silently got None back and failed later with an AttributeError far from the cause. Raise a ValueError naming common_time() as the fix, and report the offending lengths. Fixes #702 --- pyleoclim/core/multipleseries.py | 7 +++++-- pyleoclim/tests/test_core_MultipleSeries.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pyleoclim/core/multipleseries.py b/pyleoclim/core/multipleseries.py index 21c5e7a2..04925cab 100644 --- a/pyleoclim/core/multipleseries.py +++ b/pyleoclim/core/multipleseries.py @@ -1057,8 +1057,11 @@ def pca(self,weights=None, name=None, missing='fill-em',tol_em=5e-03, max_em_ite ''' flag, lengths = self.equal_lengths() - if flag==False: - print('All Time Series should be of same length. Apply common_time() first') + if flag == False: + raise ValueError( + "All series must be of the same length to perform PCA; " + f"got lengths {lengths}. Apply common_time() first." + ) else: # if all series have equal length p = len(lengths) n = lengths[0] diff --git a/pyleoclim/tests/test_core_MultipleSeries.py b/pyleoclim/tests/test_core_MultipleSeries.py index e319ed29..7f92d135 100644 --- a/pyleoclim/tests/test_core_MultipleSeries.py +++ b/pyleoclim/tests/test_core_MultipleSeries.py @@ -313,6 +313,19 @@ def test_pca_t3(self, gen_ts): # check that all variance was recovered assert abs(res.pctvar.sum() - 100) < 0.001 + def test_pca_t4(self, gen_ts): + """ + Series of unequal length must raise, not return None + + """ + signal = gen_ts(model="colored_noise", nt=100, alpha=1.0).standardize() + long = pyleo.Series(time=np.arange(100), value=signal.value) + short = pyleo.Series(time=np.arange(80), value=signal.value[:80]) + ms = pyleo.MultipleSeries([long, short]) + + with pytest.raises(ValueError, match="common_time"): + ms.pca() + class TestMultipleSeriesIncrements: """Test for MultipleSeries.increments()"""