Skip to content

Commit

Permalink
Merge pull request #350 from MarcoGorelli/optional-common-time
Browse files Browse the repository at this point in the history
Add use_common_time argument to MultipleSeries.to_pandas
  • Loading branch information
khider authored Mar 1, 2023
2 parents 0da854c + 4501dd2 commit cc1cd84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
13 changes: 10 additions & 3 deletions pyleoclim/core/multipleseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,7 @@ def stripes(self, ref_period=None, figsize=None, savefig_settings=None,
mpl.rcParams.update(current_style)
return ax

def to_pandas(self, *args, **kwargs):
def to_pandas(self, *args, use_common_time=False, **kwargs):
"""
Align Series and place in DataFrame.
Expand All @@ -2110,10 +2110,17 @@ def to_pandas(self, *args, **kwargs):
----------
*args, **kwargs
Arguments and keyword arguments to pass to ``common_time``.
use_common_time, bool
Pass True if you want to use ``common_time`` to align the Series
to have common times. Else, times for which some Series doesn't
have values will be filled with NaN (default).
Returns
-------
pandas.DataFrame
"""
ms_aligned = self.common_time(*args, **kwargs)
return pd.DataFrame({ser.metadata['label']: ser.to_pandas() for ser in ms_aligned.series_list})
if use_common_time:
ms = self.common_time(*args, **kwargs)
else:
ms = self
return pd.DataFrame({ser.metadata['label']: ser.to_pandas() for ser in ms.series_list})
24 changes: 19 additions & 5 deletions pyleoclim/tests/test_core_MultipleSeries.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,23 +516,37 @@ def test_remove_overload(self):
assert ms.series_list[0].equals(ts1) == (True, True)

class TestToPandas:
def test_to_pandas(self):
def test_to_pandas_with_common_time(self):
ts1 = pyleo.Series(time=np.array([1, 2, 4]), value=np.array([7, 4, 9]), time_unit='years CE', label='foo')
ts2 = pyleo.Series(time=np.array([1, 3, 4]), value=np.array([7, 8, 1]), time_unit='years CE', label='bar')
ms = pyleo.MultipleSeries([ts1, ts2])
result = ms.to_pandas()
result = ms.to_pandas(use_common_time=True)
expected_index = pd.DatetimeIndex(
np.array(['0000-12-31 05:48:45', '0002-07-02 02:31:54','0003-12-31 23:15:03'], dtype='datetime64[s]'),
name='datetime',
)
expected = pd.DataFrame({'foo': [7, 5.25,9.00], 'bar': [7, 7.75,1.00]}, index=expected_index)
pd.testing.assert_frame_equal(result, expected)
def test_to_pandas_args_kwargs(self):

def test_to_pandas_defau(self):
ts1 = pyleo.Series(time=np.array([1, 2, 4]), value=np.array([7, 4, 9]), time_unit='years CE', label='foo')
ts2 = pyleo.Series(time=np.array([1, 3, 4]), value=np.array([7, 8, 1]), time_unit='years CE', label='bar')
ms = pyleo.MultipleSeries([ts1, ts2])
result = ms.to_pandas('bin', start=2)
result = ms.to_pandas()
expected_index = pd.DatetimeIndex(
np.array(['0000-12-31 05:48:45', '0001-12-31 11:37:31', '0002-12-31 17:26:17', '0003-12-31 23:15:03'],
dtype='datetime64[s]'),
name='datetime',
freq='31556926S',
)
expected = pd.DataFrame({'foo': [7, 4, np.nan, 9], 'bar': [7, np.nan, 8, 1]}, index=expected_index)
pd.testing.assert_frame_equal(result, expected)

def test_to_pandas_args_kwargs(self):
ts1 = pyleo.Series(time=np.array([1, 2, 4]), value=np.array([7, 4, 9]), time_unit='years CE', label='foo',verbose=False)
ts2 = pyleo.Series(time=np.array([1, 3, 4]), value=np.array([7, 8, 1]), time_unit='years CE', label='bar',verbose=False)
ms = pyleo.MultipleSeries([ts1, ts2])
result = ms.to_pandas('bin', use_common_time=True, start=2)
expected_index = pd.DatetimeIndex(
np.array(['0002-12-31 17:26:17'], dtype='datetime64[s]'),
name='datetime',
Expand Down

0 comments on commit cc1cd84

Please sign in to comment.