Skip to content

Commit 4b318f6

Browse files
committed
Fix: Update jitter initialization logic and improve test assertions for add_jitter function
1 parent 6a859dd commit 4b318f6

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

doubleml/did/utils/_plot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def add_jitter(data, x_col, is_datetime=None, jitter_value=None):
2525
is_datetime = pd.api.types.is_datetime64_any_dtype(data[x_col])
2626

2727
# Initialize jittered_x with original values
28-
data["jittered_x"] = data[x_col]
28+
if is_datetime:
29+
data["jittered_x"] = data[x_col]
30+
else:
31+
data["jittered_x"] = data[x_col].astype(float)
2932

3033
for x_val in data[x_col].unique():
3134
mask = data[x_col] == x_val

doubleml/did/utils/tests/test_add_jitter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import datetime, timedelta
22

3+
import numpy as np
34
import pandas as pd
45
import pytest
56

@@ -41,8 +42,7 @@ def test_add_jitter_numeric_no_duplicates(numeric_df_no_duplicates):
4142
"""Test that no jitter is added when there are no duplicates."""
4243
result = add_jitter(numeric_df_no_duplicates, "x")
4344
# No jitter should be added when there are no duplicates
44-
pd.testing.assert_series_equal(result["jittered_x"], result["x"], check_names=False)
45-
45+
np.testing.assert_allclose(result["jittered_x"], result["x"])
4646

4747
@pytest.mark.ci
4848
def test_add_jitter_numeric_with_duplicates(numeric_df_with_duplicates):
@@ -121,7 +121,7 @@ def test_add_jitter_explicit_datetime_flag():
121121
df = pd.DataFrame({"x": ["2023-01-01", "2023-01-01", "2023-01-02"], "y": [10, 15, 20]})
122122

123123
# Without specifying is_datetime, it would treat as strings
124-
with pytest.raises(TypeError):
124+
with pytest.raises(ValueError):
125125
_ = add_jitter(df, "x")
126126

127127
# With is_datetime=True, it should convert and jitter as datetimes

0 commit comments

Comments
 (0)