diff --git a/pyleoclim/core/geoseries.py b/pyleoclim/core/geoseries.py index d18bdcb0..bd9a0da0 100644 --- a/pyleoclim/core/geoseries.py +++ b/pyleoclim/core/geoseries.py @@ -167,7 +167,17 @@ def __init__(self, time, value, lat, lon, elevation = None, time_unit=None, time time_name='Time' else: pass - + + if dropna: + if depth is not None: + matrix = np.array([time,value,depth]) + matrix = tsbase.dropna_matrix(matrix) + time = matrix[0,:] + value = matrix[1,:] + depth = matrix[2,:] + else: + pass + # assign latitude if lat is not None: lat = float(lat) diff --git a/pyleoclim/tests/test_core_GeoSeries.py b/pyleoclim/tests/test_core_GeoSeries.py index 01c5f412..87594d4e 100644 --- a/pyleoclim/tests/test_core_GeoSeries.py +++ b/pyleoclim/tests/test_core_GeoSeries.py @@ -45,6 +45,47 @@ def multiple_pinkgeoseries(nrecs = 20, seed = 108, geobox=[-85.0,85.0,-180,180]) return pyleo.MultipleGeoSeries(ts_list, label='Multiple Pink GeoSeries') +class TestUIGeoSeriesInit: + ''' Test for GeoSeries instantiation ''' + + def test_init_no_dropna_depth(self, evenly_spaced_series): + ts = evenly_spaced_series + t = ts.time + v = ts.value + d = np.arange(len(t)) + v[0] = np.nan + ts2 = pyleo.GeoSeries(time=t,value=v,depth=d,dropna=False, verbose=False,lat=0,lon=0) + assert np.isnan(ts2.value[0]) + assert ts2.depth[0] == d[0] + + def test_init_dropna_depth(self, evenly_spaced_series): + ts = evenly_spaced_series + t = ts.time + v = ts.value + d = np.arange(len(t)) + v[0] = np.nan + ts2 = pyleo.GeoSeries(time=t,value=v,depth=d,dropna=True, verbose=False,lat=0,lon=0) + print(ts2.value) + assert ~np.isnan(ts2.value[0]) + assert ts2.depth[0] == d[1] + + def test_init_no_dropna(self, evenly_spaced_series): + ts = evenly_spaced_series + t = ts.time + v = ts.value + v[0] = np.nan + ts2 = pyleo.GeoSeries(time=t,value=v,dropna=False, verbose=False,lat=0,lon=0) + assert np.isnan(ts2.value[0]) + + def test_init_dropna(self, evenly_spaced_series): + ts = evenly_spaced_series + t = ts.time + v = ts.value + v[0] = np.nan + ts2 = pyleo.GeoSeries(time=t,value=v,dropna=True, verbose=False,lat=0,lon=0) + print(ts2.value) + assert ~np.isnan(ts2.value[0]) + @pytest.mark.xfail # will fail until pandas is fixed class TestUIGeoSeriesResample(): ''' test GeoSeries.Resample() diff --git a/pyleoclim/utils/tsbase.py b/pyleoclim/utils/tsbase.py index 111e5d13..a88e2429 100644 --- a/pyleoclim/utils/tsbase.py +++ b/pyleoclim/utils/tsbase.py @@ -384,6 +384,36 @@ def dropna(ys, ts, verbose=False): return ys, ts +def dropna_matrix(matrix, verbose=False): + '''Drop NaN values from matrix + + Remove columns from matrix that contain nans + + Parameters + ---------- + matrix : array + A matrix, NaNs allowed + verbose : bool + If True, will print a warning message + + Returns + ------- + matrix : array + The matrix without nans + + See Also + -------- + + https://pandas.pydata.org/docs/reference/api/pandas.Series.dropna.html + + ''' + matrix = matrix[:, ~np.isnan(matrix).any(axis=0)] + + if verbose and any(np.isnan(matrix)): + print('NaNs have been detected and dropped.') + + return matrix + def sort_ts(ys, ts, ascending = True, verbose=False): ''' Sort timeseries