Skip to content

Commit c2535d1

Browse files
committed
simplify navdata.pandas_df() to allow it to work with empty NavData structures
1 parent 1b5da55 commit c2535d1

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

gnss_lib_py/parsers/navdata.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ def pandas_df(self):
154154
df : pd.DataFrame
155155
DataFrame with data, including strings as strings
156156
"""
157+
157158
df_list = []
158-
for key, value in self.str_map.items():
159-
if value:
160-
vect_val = self.get_strings(key)
161-
else:
162-
vect_val = self.array[self.map[key], :]
163-
df_val = pd.DataFrame(vect_val, columns=[key])
164-
df_list.append(df_val)
165-
dframe = pd.concat(df_list, axis=1)
159+
for row in self.rows:
160+
df_list.append(np.atleast_1d(self[row]))
161+
162+
# transpose list to conform to Pandas input
163+
df_list = [list(x) for x in zip(*df_list)]
164+
165+
dframe = pd.DataFrame(df_list,columns=self.rows)
166+
166167
return dframe
167168

168169
def get_strings(self, key):

tests/parsers/test_navdata.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ def fixture_df_int_first(csv_int_first):
148148
"""
149149
return load_test_dataframe(csv_int_first)
150150

151+
@pytest.fixture(name='df_only_header')
152+
def fixture_df_only_header(csv_only_header):
153+
"""df where only headers given and no data.
154+
155+
"""
156+
return load_test_dataframe(csv_only_header)
157+
151158
@pytest.fixture(name="data")
152159
def load_test_navdata(df_simple):
153160
"""Creates a NavData instance from df_simple.
@@ -1716,3 +1723,40 @@ def test_in_rows_multi(data):
17161723
data_temp.in_rows(combo_rows)
17171724
for row in combo_rows:
17181725
assert row in str(excinfo.value)
1726+
1727+
def test_pandas_df(df_simple, df_only_header):
1728+
"""Test that the NavData class can be printed without errors
1729+
1730+
Parameters
1731+
----------
1732+
df_simple : pd.DataFrame
1733+
Dataframe with which to construct NavData instance
1734+
df_only_header : pd.DataFrame
1735+
Dataframe with only column names and no data
1736+
"""
1737+
navdata = NavData(pandas_df=df_simple)
1738+
1739+
# test simple DataFrame
1740+
pd.testing.assert_frame_equal(navdata.pandas_df().sort_index(axis=1),
1741+
df_simple.sort_index(axis=1),
1742+
check_names=True, check_dtype=False)
1743+
1744+
# test DataFrame with a single row of data
1745+
df_super_simple = pd.DataFrame([["first",1.,3,"fourth"]],
1746+
columns=["A","B","C","D"])
1747+
navdata = NavData(pandas_df=df_super_simple)
1748+
pd.testing.assert_frame_equal(navdata.pandas_df().sort_index(axis=1),
1749+
df_super_simple.sort_index(axis=1),
1750+
check_names=True, check_dtype=False)
1751+
1752+
# make sure print doesn't break if given only headers
1753+
navdata = NavData(pandas_df=df_only_header)
1754+
pd.testing.assert_frame_equal(navdata.pandas_df().sort_index(axis=1),
1755+
df_only_header.sort_index(axis=1),
1756+
check_names=True)
1757+
1758+
# make sure it doesn't break with empty NavData
1759+
navdata = NavData()
1760+
pd.testing.assert_frame_equal(navdata.pandas_df().sort_index(axis=1),
1761+
pd.DataFrame().sort_index(axis=1),
1762+
check_names=True)

0 commit comments

Comments
 (0)