Skip to content

BUG: Refactor test_dti_cmp_tdi_tzawareness function to reduce redundancy pandas/tests/arithmetic/test_datetime64.py #58943

@himanshurd

Description

@himanshurd

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

def test_dti_cmp_tdi_tzawareness(self, other):
    # GH#22074
    # Reversion test that ensures we do not call _assert_tzawareness_compat
    # when comparing a DatetimeIndex against a TimedeltaIndex

    # Create a DatetimeIndex with a timezone 'Asia/Tokyo', spanning 10 periods
    dti = date_range("2000-01-01", periods=10, tz="Asia/Tokyo")

    # Comparison operations and expected results
    comparison_tests = {
        '==': np.array([False] * 10),
        '!=': np.array([True] * 10)
    }

    # Loop through each comparison operation and expected result
    for op, expected in comparison_tests.items():
        # Perform the comparison dynamically
        result = eval(f'dti {op} other')
        # Assert that the result matches the expected array
        tm.assert_numpy_array_equal(result, expected)

    # Define the error message expected for invalid comparisons
    msg = "Invalid comparison between"
    # List of comparison operators that should raise a TypeError
    invalid_comparisons = ['<', '<=', '>', '>=']

    # Loop through invalid comparisons to assert TypeError is raised
    for op in invalid_comparisons:
        with pytest.raises(TypeError, match=msg):
            eval(f'dti {op} other')

Issue Description

The function had repeated sections for testing equality (== and !=) and invalid comparisons (<, <=, >, >=), which resulted in a lot of duplicated code.

Expected Behavior

The behavior will remain same

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions