Skip to content

Commit bb29f1b

Browse files
committed
Refactoring lambda function
1 parent 2a212d8 commit bb29f1b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

pandas/io/sas/sas7bdat.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,25 @@
2323
from pandas.errors import EmptyDataError, OutOfBoundsDatetime
2424

2525
import pandas as pd
26-
from pandas import notna
26+
from pandas import isnull
2727

2828
from pandas.io.common import get_handle
2929
from pandas.io.sas._sas import Parser
3030
import pandas.io.sas.sas_constants as const
3131
from pandas.io.sas.sasreader import ReaderBase
3232

3333

34+
def _parse_datetime(sas_datetime: float, unit: str):
35+
if isnull(sas_datetime):
36+
return pd.NaT
37+
38+
if unit == "s":
39+
return datetime(1960, 1, 1) + timedelta(seconds=sas_datetime)
40+
41+
if unit == "d":
42+
return datetime(1960, 1, 1) + timedelta(days=sas_datetime)
43+
44+
3445
def _convert_datetimes(sas_datetimes: pd.Series, unit: str) -> pd.Series:
3546
"""
3647
Convert to Timestamp if possible, otherwise to datetime.datetime.
@@ -52,22 +63,10 @@ def _convert_datetimes(sas_datetimes: pd.Series, unit: str) -> pd.Series:
5263
try:
5364
return pd.to_datetime(sas_datetimes, unit=unit, origin="1960-01-01")
5465
except OutOfBoundsDatetime:
55-
if unit == "s":
56-
s_series = sas_datetimes.apply(
57-
lambda sas_float: datetime(1960, 1, 1) + timedelta(seconds=sas_float)
58-
if notna(sas_float)
59-
else pd.NaT
60-
)
66+
if unit in ["s", "d"]:
67+
s_series = sas_datetimes.apply(_parse_datetime, unit=unit)
6168
s_series = cast(pd.Series, s_series)
6269
return s_series
63-
elif unit == "d":
64-
d_series = sas_datetimes.apply(
65-
lambda sas_float: datetime(1960, 1, 1) + timedelta(days=sas_float)
66-
if notna(sas_float)
67-
else pd.NaT
68-
)
69-
d_series = cast(pd.Series, d_series)
70-
return d_series
7170
else:
7271
raise ValueError("unit must be 'd' or 's'")
7372

0 commit comments

Comments
 (0)