Skip to content

Commit

Permalink
Error if relationship child variable is child entity index (#1034)
Browse files Browse the repository at this point in the history
* error if child variable is child index

* changelog

* update error message
  • Loading branch information
frances-h committed Jun 26, 2020
1 parent a71d19c commit 95117a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Expand Up @@ -7,6 +7,7 @@ Changelog
* Add ``list_variable_types`` and ``graph_variable_types`` for Variable Types (:pr:`1013`)
* Fixes
* Improve warnings when using a Dask dataframe for cutoff times (:pr:`1026`)
* Error if attempting to add entityset relationship where child variable is also child index (:pr:`1034`)
* Changes
* Remove ``Feature.get_names`` (:pr:`1021`)
* Remove unnecessary ``pd.Series`` and ``pd.DatetimeIndex`` calls from primitives (:pr:`1020`, :pr:`1024`)
Expand Down
3 changes: 3 additions & 0 deletions featuretools/entityset/entityset.py
Expand Up @@ -248,6 +248,9 @@ def add_relationship(self, relationship):
# this is a new pair of entities
child_e = relationship.child_entity
child_v = relationship.child_variable.id
if child_e.index == child_v:
msg = "Unable to add relationship because child variable '{}' in '{}' is also its index"
raise ValueError(msg.format(child_v, child_e.id))
parent_e = relationship.parent_entity
parent_v = relationship.parent_variable.id
if not isinstance(child_e[child_v], vtypes.Id):
Expand Down
19 changes: 17 additions & 2 deletions featuretools/tests/entityset_tests/test_es.py
Expand Up @@ -135,6 +135,21 @@ def test_add_relationship_errors_on_dtype_mismatch(es):
es.add_relationship(mismatch)


def test_add_relationship_errors_child_v_index(es):
log_df = es['log'].df.copy()
log_vtypes = es['log'].variable_types
es.entity_from_dataframe(entity_id='log2',
dataframe=log_df,
index='id',
variable_types=log_vtypes,
time_index='datetime')

bad_relationship = ft.Relationship(es['log']['id'], es['log2']['id'])
to_match = "Unable to add relationship because child variable 'id' in 'log2' is also its index"
with pytest.raises(ValueError, match=to_match):
es.add_relationship(bad_relationship)


def test_add_relationship_empty_child_convert_dtype(es):
relationship = ft.Relationship(es["sessions"]["id"], es["log"]["session_id"])
es['log'].df = pd.DataFrame(columns=es['log'].df.columns)
Expand Down Expand Up @@ -1327,7 +1342,7 @@ def test_entityset_init():
"transactions": (transactions_df, 'id', 'transaction_time',
variable_types, False)
}
relationships = [('cards', 'id', 'transactions', 'id')]
relationships = [('cards', 'id', 'transactions', 'card_id')]
es = ft.EntitySet(id="fraud_data",
entities=entities,
relationships=relationships)
Expand All @@ -1344,7 +1359,7 @@ def test_entityset_init():
make_index=False,
time_index='transaction_time')
relationship = ft.Relationship(es_copy["cards"]["id"],
es_copy["transactions"]["id"])
es_copy["transactions"]["card_id"])
es_copy.add_relationship(relationship)
assert es['cards'].__eq__(es_copy['cards'], deep=True)
assert es['transactions'].__eq__(es_copy['transactions'], deep=True)

0 comments on commit 95117a7

Please sign in to comment.