Skip to content

Commit

Permalink
added unit test for df_from_dict indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensle committed Feb 8, 2024
1 parent e8cb5f4 commit 70ab2cc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion activitysim/core/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas.testing as pdt
import pytest

from ..util import other_than, quick_loc_df, quick_loc_series, reindex
from ..util import other_than, quick_loc_df, quick_loc_series, reindex, df_from_dict


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -62,3 +62,30 @@ def test_quick_loc_series():

assert list(quick_loc_series(loc_list, series)) == attrib_list
assert list(quick_loc_series(loc_list, series)) == list(series.loc[loc_list])


def test_df_from_dict():

index = [1, 2, 3, 4, 5]
df = pd.DataFrame({"attrib": [1, 2, 2, 3, 1]}, index=index)

# scramble index order for one expression and not the other
sorted = df.eval("attrib.sort_values()")
not_sorted = df.eval("attrib * 1")

# check above expressions
pdt.assert_series_equal(
sorted, pd.Series([1, 1, 2, 2, 3], index=[1, 5, 2, 3, 4]), check_names=False
)
pdt.assert_series_equal(not_sorted, df.attrib, check_names=False)

# create a new dataframe from the above expressions
values = {"sorted": sorted, "not_sorted": not_sorted}
new_df = df_from_dict(values, index)

# index should become unscrambed and back to the same order as before
expected_df = pd.DataFrame(
{"sorted": [1, 2, 2, 3, 1], "not_sorted": [1, 2, 2, 3, 1]}, index=index
)

pdt.assert_frame_equal(new_df, expected_df)

0 comments on commit 70ab2cc

Please sign in to comment.