Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] pandas_compat: do not parse column of numbers (object dtype) to datetime #5681

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Orange/data/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def _is_datetime(s):
return True
try:
if is_object_dtype(s):
# pd.to_datetime would sucessfuly parse column of numbers to datetime
# but for column of object dtype with numbers we want to be either
# discret or string - following code try to parse column to numeric
# if connversion to numeric is sucessful return False
try:
pd.to_numeric(s)
return False
except (ValueError, TypeError):
pass

# utc=True - to allow different timezones in a series object
pd.to_datetime(s, infer_datetime_format=True, utc=True)
return True
Expand Down
19 changes: 19 additions & 0 deletions Orange/data/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ def test_table_from_frame_timezones(self):
],
)

def test_table_from_frame_no_datetim(self):
"""
In case when dtype of column is object and column contains numbers only,
column could be recognized as a TimeVarialbe since pd.to_datetime can parse
numbers as datetime. That column must be result either in StringVariable
or DiscreteVariable since it's dtype is object.
"""
from Orange.data.pandas_compat import table_from_frame

df = pd.DataFrame([[1], [2], [3]], dtype="object")
table = table_from_frame(df)
# check if exactly ContinuousVariable and not subtype TimeVariable
self.assertIsInstance(table.domain.metas[0], StringVariable)

df = pd.DataFrame([[1], [2], [2]], dtype="object")
table = table_from_frame(df)
# check if exactly ContinuousVariable and not subtype TimeVariable
self.assertIsInstance(table.domain.attributes[0], DiscreteVariable)

def test_time_variable_compatible(self):
from Orange.data.pandas_compat import table_from_frame

Expand Down