Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frances-h committed Jun 27, 2023
1 parent 43eae6b commit b3094ae
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/integration/transformers/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,47 @@ def test_unixtimestampencoder_with_model_missing_values(self):
pd.testing.assert_frame_equal(expected_transformed, transformed)
pd.testing.assert_frame_equal(reverted, data)

def test_unixtimestampencoder_with_integer_datetimes(self):
"""Test that the transformer properly handles integer columns."""
# Setup
ute = UnixTimestampEncoder('mean', True, datetime_format='%m%d%Y')
data = pd.DataFrame({'column': [1201992, 11022028, 10011990]})

# Run
ute.fit(data, column='column')
ute.set_random_state(np.random.RandomState(7), 'reverse_transform')
transformed = ute.transform(data)
reverted = ute.reverse_transform(transformed)

# Asserts
expected_transformed = pd.DataFrame({
'column': [6.958656e+17, 1.856736e+18, 6.547392e+17],
})

pd.testing.assert_frame_equal(expected_transformed, transformed)
pd.testing.assert_frame_equal(reverted, data)

def test_unixtimestampencoder_with_nans(self):
"""Test that the transformer properly handles null columns."""
# Setup
ute = UnixTimestampEncoder('mean', True)
data = pd.DataFrame({'column': [np.nan, np.nan, np.nan]})

# Run
ute.fit(data, column='column')
ute.set_random_state(np.random.RandomState(7), 'reverse_transform')
transformed = ute.transform(data)
reverted = ute.reverse_transform(transformed)

# Asserts
expected_transformed = pd.DataFrame({
'column': [0., 0., 0.],
'column.is_null': [1., 1., 1.]
})

pd.testing.assert_frame_equal(expected_transformed, transformed)
pd.testing.assert_frame_equal(reverted, data)


class TestOptimizedTimestampEncoder:
def test_optimizedtimestampencoder(self):
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/transformers/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,39 @@ def test__reverse_transform_datetime_format_with_strftime_formats(self):
if 'windows' not in platform.system().lower():
pd.testing.assert_series_equal(output, expected)

def test__reverse_transform_datetime_format_with_nans(self):
"""Test the ``_reverse_transform`` method returns the correct datetime format with nans."""
# Setup
ute = UnixTimestampEncoder()
ute.datetime_format = '%b %-d, %Y'
transformed = np.array([1.5778368e+18, 1.5805152e+18, np.nan])
ute._dtype = 'object'
ute.null_transformer = NullTransformer('mean')

# Run
output = ute._reverse_transform(transformed)

# Assert
expected = pd.Series(['Jan 1, 2020', 'Feb 1, 2020', np.nan])
if 'windows' not in platform.system().lower():
pd.testing.assert_series_equal(output, expected)

def test__reverse_transform_only_nans(self):
"""Test the ``_reverse_transform`` method returns the correct datetime format with nans."""
# Setup
ute = UnixTimestampEncoder()
transformed = np.array([np.nan, np.nan, np.nan])
ute._dtype = 'float'
ute.null_transformer = NullTransformer('mean')

# Run
output = ute._reverse_transform(transformed)

# Assert
expected = pd.Series([np.nan, np.nan, np.nan])
if 'windows' not in platform.system().lower():
pd.testing.assert_series_equal(output, expected)


class TestOptimizedTimestampEncoder:

Expand Down

0 comments on commit b3094ae

Please sign in to comment.