Skip to content

Commit 5b0be83

Browse files
committed
Add compat.py module
1 parent ef5f01a commit 5b0be83

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

src/estimagic/compat.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Compatibility module.
2+
3+
Contains wrapper functions to handle compatibility issues between different versions of
4+
external libraries.
5+
6+
"""
7+
8+
from estimagic.config import IS_PANDAS_VERSION_NEWER_OR_EQUAL_TO_2_1_0
9+
10+
11+
def pandas_df_map(df, func, na_action=None, **kwargs):
12+
"""Apply a function to a Dataframe elementwise.
13+
14+
pandas has depricated the .applymap() function with version 2.1.0. This function
15+
calls either .map() (if pandas version is greater or equal to 2.1.0) or .applymap()
16+
(if pandas version is smaller than 2.1.0).
17+
18+
Args:
19+
df (pd.DataFrame): A pandas DataFrame.
20+
func (callable): Python function, returns a single value from a single value.
21+
na_action (str): If 'ignore', propagate NaN values, without passing them to
22+
func. If None, pass NaN values to func. Default is None.
23+
**kwargs: Additional keyword arguments to pass as keywords arguments to func.
24+
25+
Returns:
26+
pd.DataFrame: Transformed DataFrame.
27+
28+
"""
29+
if IS_PANDAS_VERSION_NEWER_OR_EQUAL_TO_2_1_0:
30+
out = df.map(func, na_action=na_action, **kwargs)
31+
else:
32+
out = df.applymap(func, na_action=na_action, **kwargs)
33+
return out

src/estimagic/optimization/optimize_result.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import numpy as np
55
import pandas as pd
66

7-
from estimagic.utilities import to_pickle, pandas_df_map
7+
from estimagic.utilities import to_pickle
8+
from estimagic.compat import pandas_df_map
89

910

1011
@dataclass

src/estimagic/utilities.py

-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pandas as pd
88
from scipy.linalg import ldl, qr
9-
from estimagic.config import IS_PANDAS_VERSION_NEWER_OR_EQUAL_TO_2_1_0
109

1110
with warnings.catch_warnings():
1211
warnings.simplefilter("ignore", category=UserWarning)
@@ -322,28 +321,3 @@ def get_rng(seed):
322321
else:
323322
raise TypeError("seed type must be in {None, int, numpy.random.Generator}.")
324323
return rng
325-
326-
327-
def pandas_df_map(df, func, na_action=None, **kwargs):
328-
"""Apply a function to a Dataframe elementwise.
329-
330-
pandas has depricated the .applymap() function with version 2.1.0. This function
331-
calls either .map() (if pandas version is greater or equal to 2.1.0) or .applymap()
332-
(if pandas version is smaller than 2.1.0).
333-
334-
Args:
335-
df (pd.DataFrame): A pandas DataFrame.
336-
func (callable): Python function, returns a single value from a single value.
337-
na_action (str): If 'ignore', propagate NaN values, without passing them to
338-
func. If None, pass NaN values to func. Default is None.
339-
**kwargs: Additional keyword arguments to pass as keywords arguments to func.
340-
341-
Returns:
342-
pd.DataFrame: Transformed DataFrame.
343-
344-
"""
345-
if IS_PANDAS_VERSION_NEWER_OR_EQUAL_TO_2_1_0:
346-
out = df.map(func, na_action=na_action, **kwargs)
347-
else:
348-
out = df.applymap(func, na_action=na_action, **kwargs)
349-
return out

src/estimagic/visualization/estimation_table.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import partial
44
from pathlib import Path
55
from warnings import warn
6-
from estimagic.utilities import pandas_df_map
6+
from estimagic.compat import pandas_df_map
77

88
import numpy as np
99
import pandas as pd

0 commit comments

Comments
 (0)