Skip to content

Commit

Permalink
Merge pull request #222 from UDST/update_pandas
Browse files Browse the repository at this point in the history
Compatibility with Pandas v1.0
  • Loading branch information
smmaurer authored Mar 23, 2020
2 parents 79f815a + c9b4911 commit 84e7d3f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 39 deletions.
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: python
sudo: false
python:
- '2.7'
- '3.5'
- '3.6'
- '3.8'
install:
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh
-O miniconda.sh; else wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
Expand All @@ -16,11 +17,12 @@ install:
- >
conda create -q -n test-environment
python=$TRAVIS_PYTHON_VERSION
cytoolz ipython-notebook jinja2 matplotlib numpy pandas patsy pip scipy
statsmodels pytables pytest pyyaml toolz
cytoolz jupyter jinja2 matplotlib numpy pandas patsy pip scipy
statsmodels pytables pyyaml toolz
- source activate test-environment
- pip install orca osmnet pandana bottle simplejson zbox
- pip install pytest-cov coveralls pycodestyle
- conda install pandana -c conda-forge
- pip install orca osmnet pandana bottle simplejson zbox prettytable
- pip install 'pytest<4.0' pytest-cov coveralls pycodestyle
- pip install .
before_script:
- git clone https://github.com/udst/sanfran_urbansim.git
Expand Down
8 changes: 4 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ environment:
matrix:
- PYTHON_VERSION: 2.7
MINICONDA: C:\Miniconda
- PYTHON_VERSION: 3.5
- PYTHON_VERSION: 3.6
MINICONDA: C:\Miniconda3

init:
Expand All @@ -15,10 +15,10 @@ install:
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda info -a
- "conda create -q -n test-environment python=%PYTHON_VERSION% cytoolz ipython-notebook jinja2 matplotlib numpy pandas patsy pip scipy statsmodels pytables pytest pyyaml toolz"
- "conda create -q -n test-environment python=%PYTHON_VERSION% cytoolz jupyter jinja2 matplotlib numpy pandas patsy pip scipy statsmodels pytables pyyaml toolz"
- activate test-environment
- conda install -c conda-forge shapely geopandas
- pip install orca osmnet pandana bottle simplejson zbox
- conda install -c conda-forge shapely geopandas pytest=3.10
- pip install orca pandana osmnet bottle simplejson zbox prettytable
- pip install pycodestyle
- pip install .

Expand Down
8 changes: 4 additions & 4 deletions urbansim/models/dcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ def fit(self, choosers, alternatives, current_choice):
model_design = dmatrix(
self.str_model_expression, data=merged, return_type='dataframe')

if len(merged) != model_design.as_matrix().shape[0]:
if len(merged) != model_design.values.shape[0]:
raise ModelEvaluationError(
'Estimated data does not have the same length as input. '
'This suggests there are null values in one or more of '
'the input columns.')

self.log_likelihoods, self.fit_parameters = mnl.mnl_estimate(
model_design.as_matrix(), chosen, self.sample_size)
model_design.values, chosen, self.sample_size)
self.fit_parameters.index = model_design.columns

logger.debug('finish: fit LCM model {}'.format(self.name))
Expand Down Expand Up @@ -523,7 +523,7 @@ def probabilities(self, choosers, alternatives, filter_tables=True):
model_design = dmatrix(
self.str_model_expression, data=merged, return_type='dataframe')

if len(merged) != model_design.as_matrix().shape[0]:
if len(merged) != model_design.values.shape[0]:
raise ModelEvaluationError(
'Simulated data does not have the same length as input. '
'This suggests there are null values in one or more of '
Expand All @@ -542,7 +542,7 @@ def probabilities(self, choosers, alternatives, filter_tables=True):
numalts = sample_size

probabilities = mnl.mnl_simulate(
model_design.as_matrix(),
model_design.values,
coeffs,
numalts=numalts, returnprobs=True)

Expand Down
4 changes: 2 additions & 2 deletions urbansim/urbanchoice/tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_interaction_dataset_sim(choosers, alternatives):

assert len(merged) == len(choosers) * len(alternatives)
npt.assert_array_equal(merged.index.values, sample)
assert list(merged.columns) == [
'var2', 'var3', 'join_index', 'thing_id', 'var1']
assert set(list(merged.columns)) == set([
'var2', 'var3', 'join_index', 'thing_id', 'var1'])
npt.assert_array_equal(
merged['var1'].values,
choosers['var1'].values.repeat(len(alternatives)))
Expand Down
14 changes: 7 additions & 7 deletions urbansim/urbanchoice/tests/test_mnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def choosers_dm(choosers, test_data):

@pytest.fixture
def fit_coeffs(dm, chosen, num_alts):
log_like, fit = mnl.mnl_estimate(dm.as_matrix(), chosen, num_alts)
log_like, fit = mnl.mnl_estimate(dm.values, chosen, num_alts)
return fit.Coefficient.values


def test_mnl_estimate(dm, chosen, num_alts, test_data):
log_like, fit = mnl.mnl_estimate(dm.as_matrix(), chosen, num_alts)
log_like, fit = mnl.mnl_estimate(dm.values, chosen, num_alts)
result = pd.Series(fit.Coefficient.values, index=dm.columns)
result, expected = result.align(test_data['est_expected'])
npt.assert_allclose(result.values, expected.values, rtol=1e-4)
Expand All @@ -134,10 +134,10 @@ def test_mnl_simulate(dm, fit_coeffs, num_alts, test_data, choosers_dm):

# now test with real data
probs = mnl.mnl_simulate(
choosers_dm.as_matrix(), fit_coeffs, num_alts, returnprobs=True)
choosers_dm.values, fit_coeffs, num_alts, returnprobs=True)
results = pd.DataFrame(probs, columns=test_data['sim_expected'].columns)
results, expected = results.align(test_data['sim_expected'])
npt.assert_allclose(results.as_matrix(), expected.as_matrix(), rtol=1e-4)
npt.assert_allclose(results.values, expected.values, rtol=1e-4)


def test_alternative_specific_coeffs(num_alts):
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_alternative_specific_coeffs(num_alts):
'boat:(intercept)', 'charter:(intercept)', 'pier:(intercept)',
'boat:income', 'charter:income', 'pier:income'])

log_like, fit = mnl.mnl_estimate(dm.as_matrix(), fish_chosen, num_alts)
log_like, fit = mnl.mnl_estimate(dm.values, fish_chosen, num_alts)
result = pd.Series(fit.Coefficient.values, index=dm.columns)
result, expected = result.align(expected)
npt.assert_allclose(result.values, expected.values, rtol=1e-4)
Expand All @@ -206,7 +206,7 @@ def test_alternative_specific_coeffs(num_alts):

fit_coeffs = fit.Coefficient.values
probs = mnl.mnl_simulate(
choosers_dm.as_matrix(), fit_coeffs, num_alts, returnprobs=True)
choosers_dm.values, fit_coeffs, num_alts, returnprobs=True)
results = pd.DataFrame(probs, columns=expected.columns)
results, expected = results.align(expected)
npt.assert_allclose(results.as_matrix(), expected.as_matrix(), rtol=1e-4)
npt.assert_allclose(results.values, expected.values, rtol=1e-4)
4 changes: 2 additions & 2 deletions urbansim/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def compute_range(travel_data, attr, travel_time_attr, dist, agg=np.sum):
"""
travel_data = travel_data.reset_index(level=1)
travel_data = travel_data[travel_data[travel_time_attr] < dist]
travel_data["attr"] = attr[travel_data.to_zone_id].values
travel_data["attr"] = attr.reindex(travel_data.to_zone_id, fill_value=0).values
return travel_data.groupby(level=0).attr.apply(agg)


Expand Down Expand Up @@ -357,7 +357,7 @@ def series64bitto32bit(s):


def _pandassummarytojson(v, ndigits=3):
return {i: round(float(v.ix[i]), ndigits) for i in v.index}
return {i: round(float(v.loc[i]), ndigits) for i in v.index}


def pandasdfsummarytojson(df, ndigits=3):
Expand Down
30 changes: 15 additions & 15 deletions urbansim/utils/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@ def test_frames_equal_mismatched_columns():
expected = pd.DataFrame({'a': [1]})
actual = pd.DataFrame({'b': [2]})

with pytest.raises(AssertionError) as info:
try:
testing.assert_frames_equal(actual, expected)

assert str(info.value) == "Expected column 'a' not found."
except AssertionError:
pass
else:
raise AssertionError


def test_frames_equal_mismatched_rows():
expected = pd.DataFrame({'a': [1]}, index=[0])
actual = pd.DataFrame({'a': [1]}, index=[1])

with pytest.raises(AssertionError) as info:
try:
testing.assert_frames_equal(actual, expected)

assert str(info.value) == "Expected row 0 not found."
except AssertionError:
pass
else:
raise AssertionError


def test_frames_equal_mismatched_items():
expected = pd.DataFrame({'a': [1]})
actual = pd.DataFrame({'a': [2]})

with pytest.raises(AssertionError) as info:
try:
testing.assert_frames_equal(actual, expected)

assert str(info.value) == """
Items are not equal:
ACTUAL: 2
DESIRED: 1
Column: 'a'
Row: 0"""
except AssertionError:
pass
else:
raise AssertionError


def test_frames_equal():
Expand Down

0 comments on commit 84e7d3f

Please sign in to comment.