Skip to content

Commit 22cc5e7

Browse files
authored
Add option to force pandas (#23)
1 parent d67c9a8 commit 22cc5e7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/pymadng/madp_classes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,16 @@ def __next__(self):
202202
except IndexError:
203203
raise StopIteration
204204

205-
def to_df(self, columns: list = None): # For backwards compatibility (jgray 2024)
205+
def to_df(self, columns: list = None, force_pandas: bool = False): # For backwards compatibility (jgray 2024)
206206
"""See `convert_to_dataframe`"""
207-
return self.convert_to_dataframe(columns)
207+
return self.convert_to_dataframe(columns, force_pandas)
208208

209-
def convert_to_dataframe(self, columns: list = None):
209+
def convert_to_dataframe(self, columns: list = None, force_pandas: bool = False):
210210
"""Converts the object to a pandas dataframe.
211211
212-
This function imports pandas and tfs-pandas, if tfs-pandas is not installed, it will only return a pandas dataframe.
213-
214212
Args:
215213
columns (list, optional): List of columns to include in the dataframe. Defaults to None.
214+
force_pandas (bool, optional): If True, always use pandas.DataFrame. Defaults to False.
216215
217216
Returns:
218217
pandas.DataFrame or tfs.TfsDataFrame: The dataframe containing the object's data.
@@ -230,7 +229,10 @@ def convert_to_dataframe(self, columns: list = None):
230229
# If tfs is available, use the headers attribute
231230
DataFrame, hdr_attr = tfs.TfsDataFrame, "headers"
232231
except ImportError:
233-
# Otherwise, use the pandas dataframe and attrs attribute
232+
force_pandas = True
233+
234+
if force_pandas:
235+
# If pandas is the only option, use pandas DataFrame, with the attrs attribute
234236
DataFrame, hdr_attr = pd.DataFrame, "attrs"
235237

236238
py_name, obj_name = self._mad.py_name, self._name

tests/test_object_wrapping.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
import numpy as np
7-
import pandas
7+
import pandas as pd
88
import tfs
99

1010
from pymadng import MAD
@@ -369,7 +369,7 @@ def test_history(self):
369369

370370

371371
class TestDataFrame(unittest.TestCase):
372-
def generalDataFrame(self, headers, DataFrame):
372+
def generalDataFrame(self, headers, DataFrame, force_pandas=False):
373373
mad = MAD()
374374
mad.send("""
375375
test = mtable{
@@ -394,7 +394,7 @@ def generalDataFrame(self, headers, DataFrame):
394394
test:addcol("generator", \\ri, m -> m:getcol("number")[ri] + 1i * m:getcol("number")[ri])
395395
test:write("test")
396396
""")
397-
df = mad.test.to_df()
397+
df = mad.test.to_df(force_pandas=force_pandas)
398398
self.assertTrue(isinstance(df, DataFrame))
399399
header = getattr(df, headers)
400400
self.assertEqual(header["name"], "test")
@@ -431,9 +431,12 @@ def test_tfsDataFrame(self):
431431

432432
def test_pandasDataFrame(self):
433433
sys.modules["tfs"] = None # Remove tfs-pandas
434-
self.generalDataFrame("attrs", pandas.DataFrame)
434+
self.generalDataFrame("attrs", pd.DataFrame)
435435
del sys.modules["tfs"]
436436

437+
def test_tfsDataFrame_force_pandas(self):
438+
self.generalDataFrame("attrs", pd.DataFrame, force_pandas=True)
439+
437440
def test_failure(self):
438441
with MAD() as mad:
439442
mad.send("""

0 commit comments

Comments
 (0)