Skip to content

Commit

Permalink
Merge pull request #243 from quant12345/map
Browse files Browse the repository at this point in the history
Replacing the obsolete applymap with map.
  • Loading branch information
timkpaine authored Aug 2, 2024
2 parents ae8b822 + 8f9a0ec commit 211eb2b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ffn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import decorator
import numpy as np
import pandas as pd
from packaging.version import Version
from typing import List, Sequence, Tuple, Union

try:
Expand Down Expand Up @@ -180,7 +181,14 @@ def as_format(item: Union[pd.DataFrame, pd.Series], format_str=".2f") -> Union[p
"""
Map a format string over a pandas object.
"""
PANDAS_VERSION = Version(pd.__version__)
PANDAS_210 = PANDAS_VERSION >= Version("2.1.0")

select_map = "map"
if not PANDAS_210:
select_map = "applymap"

if isinstance(item, pd.Series):
return item.map(lambda x: format(x, format_str))
elif isinstance(item, pd.DataFrame):
return item.applymap(lambda x: format(x, format_str))
return getattr(item, select_map)(lambda x: format(x, format_str))

0 comments on commit 211eb2b

Please sign in to comment.