Skip to content

Commit

Permalink
validate reso in convert_datetime_index, fixup fixture;
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 16, 2023
1 parent f1430bd commit 545eaf3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyleoclim/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def dataframe_dt():
"""Pandas dataframe with a datetime index and random values"""
length = 5
dti = pd.date_range("2018-01-01", periods=length, freq="Y")
dti = pd.date_range("2018-01-01", periods=length, freq="Y", unit='s')
df = pd.DataFrame(np.array(range(length)), index=dti)
return df

Expand Down
10 changes: 5 additions & 5 deletions pyleoclim/tests/test_utils_tsutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_convert_datetime_index_ka(dataframe_dt):
time_unit,
time_name=time_name,
)
expected = np.array([-996577.75451608, -996577.75551608, -999315.60730322, -996577.75751608, -996577.75851608])
expected = np.array([-0.06899658, -0.06999658, -0.07099932, -0.07199658, -0.07299658])
assert np.allclose(time.values, expected, rtol=1e-05, atol=1e-08)


Expand All @@ -94,7 +94,7 @@ def test_convert_datetime_index_ma(dataframe_dt):
time_unit,
time_name=time_name,
)
expected = np.array([-996.57775452, -996.57775552, -999.3156073 , -996.57775752, -996.57775852])
expected = np.array([-6.89965777e-05, -6.99965777e-05, -7.09993155e-05, -7.19965777e-05, -7.29965777e-05])
assert np.allclose(time.values, expected, rtol=1e-05, atol=1e-08)


Expand All @@ -106,7 +106,7 @@ def test_convert_datetime_index_ga(dataframe_dt):
time_unit,
time_name=time_name,
)
expected = np.array([-0.99657775, -0.99657776, -0.99931561, -0.99657776, -0.99657776])
expected = np.array([-6.89965777e-08, -6.99965777e-08, -7.09993155e-08, -7.19965777e-08, -7.29965777e-08])
assert np.allclose(time.values, expected, rtol=1e-05, atol=1e-08)


Expand All @@ -118,7 +118,7 @@ def test_convert_datetime_index_bp(dataframe_dt):
time_unit,
time_name=time_name,
)
expected = np.array([-9.96577755e+08, -9.96577756e+08, -9.99315607e+08, -9.96577758e+08, -9.96577759e+08])
expected = np.array([-68.99657769, -69.99657769, -70.99931554, -71.99657769, -72.99657769])
assert np.allclose(time.values, expected, rtol=1e-05, atol=1e-08)


Expand All @@ -130,7 +130,7 @@ def test_convert_datetime_index_ad(dataframe_dt):
time_unit,
time_name=time_name,
)
expected = np.array([9.96579705e+08, 9.96579706e+08, 9.99317557e+08, 9.96579708e+08, 9.96579709e+08])
expected = np.array([2018.99657769, 2019.99657769, 2020.99931554, 2021.99657769, 2022.99657769])
assert np.allclose(time.values, expected, rtol=1e-05, atol=1e-08)


Expand Down
19 changes: 12 additions & 7 deletions pyleoclim/utils/tsutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ def convert_datetime_index_to_time(datetime_index, time_unit, time_name):

if not isinstance(datetime_index, pd.DatetimeIndex):
raise ValueError('The provided index is not a proper DatetimeIndex object')
else:
year_diff = (datetime_index.year - int(datum))
numpy_datetime_index = datetime_index.to_numpy()
years_floor = numpy_datetime_index.astype('datetime64[Y]').astype('datetime64[s]')
seconds_diff = (numpy_datetime_index - years_floor).astype('int')
diff = year_diff + seconds_diff / SECONDS_PER_YEAR
time = multiplier * diff / 10**exponent
if datetime_index.unit != 's':
raise ValueError(
"Only 'second' resolution is currently supported. "
"Please cast to second resolution with `.as_unit('s')`"
)

year_diff = (datetime_index.year - int(datum))
numpy_datetime_index = datetime_index.to_numpy()
years_floor = numpy_datetime_index.astype('datetime64[Y]').astype('datetime64[s]')
seconds_diff = (numpy_datetime_index - years_floor).astype('int')
diff = year_diff + seconds_diff / SECONDS_PER_YEAR
time = multiplier * diff / 10**exponent

return time

Expand Down

0 comments on commit 545eaf3

Please sign in to comment.