Skip to content

Commit 7bc0f6f

Browse files
committed
Adding test cases
1 parent 6f377d6 commit 7bc0f6f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

pandas/tests/test_algos.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,61 @@ def test_diff_low_precision_int(self, dtype):
20332033
expected = np.array([np.nan, 1, 0, -1, 0], dtype="float32")
20342034
tm.assert_numpy_array_equal(result, expected)
20352035

2036+
def test_diff_invalid_type_handling(self):
2037+
"""Test that diff function properly handles invalid input types"""
2038+
# Test for the bug fix where non-numeric types would raise AttributeError
2039+
# instead of ValueError
2040+
2041+
# Create a simple array for testing
2042+
arr = np.array([1, 2, 3, 4, 5])
2043+
2044+
# Test cases that should raise ValueError (not AttributeError)
2045+
invalid_inputs = [
2046+
"hello", # string
2047+
None, # None
2048+
[1, 2], # list
2049+
{"key": "value"}, # dict
2050+
object(), # generic object
2051+
]
2052+
2053+
for invalid_input in invalid_inputs:
2054+
with pytest.raises(ValueError, match="periods must be an integer"):
2055+
algos.diff(arr, invalid_input)
2056+
2057+
def test_diff_valid_float_handling(self):
2058+
"""Test that diff function properly handles valid float inputs"""
2059+
2060+
# Create a simple array for testing
2061+
arr = np.array([1, 2, 3, 4, 5])
2062+
2063+
# Test cases that should work (float values that are integers)
2064+
valid_inputs = [
2065+
1.0, # float that is an integer
2066+
2.0, # another float that is an integer
2067+
-1.0, # negative float that is an integer
2068+
]
2069+
2070+
for valid_input in valid_inputs:
2071+
# Should not raise an exception
2072+
result = algos.diff(arr, valid_input)
2073+
assert result.shape == arr.shape
2074+
2075+
def test_diff_invalid_float_handling(self):
2076+
"""Test that diff function properly handles invalid float inputs"""
2077+
2078+
# Create a simple array for testing
2079+
arr = np.array([1, 2, 3, 4, 5])
2080+
2081+
# Test cases that should raise ValueError (float values that are not integers)
2082+
invalid_float_inputs = [
2083+
1.5, # float that is not an integer
2084+
2.7, # another float that is not an integer
2085+
-1.3, # negative float that is not an integer
2086+
]
2087+
2088+
for invalid_input in invalid_float_inputs:
2089+
with pytest.raises(ValueError, match="periods must be an integer"):
2090+
algos.diff(arr, invalid_input)
20362091

20372092
@pytest.mark.parametrize("op", [np.array, pd.array])
20382093
def test_union_with_duplicates(op):

0 commit comments

Comments
 (0)