Detecting holiday fraud with Python.

Using Python's holidays library to detect fraudulent transactions on holidays.

Posted on January 12, 2024.


Holidays are fraudsters' favorite days.

In my daily grind at Verifone, fraud detection often feels like an unending strategic battle. The complexity of this challenge heightens during holidays, times when I find the products under my watch to be most vulnerable. It was in one of these bustling periods that I discovered a new facet to enhance my fraud detection project - incorporating holiday awareness.

I believe the intrigue of holidays lies in how they encapsulate the essence of duality: a time of celebration and unabashed spending, but also peak hours for fraud activity. The usual guard drops, manual checks are deferred and the business hours hustle quietly simmers down. 2023's Christmas was a stark reminder of this - what should have been a peaceful holiday

Python to the rescue.

In the pursuit of a solution, I turned to Python's holidays library, a testament to Python's philosophy of providing a tool for nearly every problem. This library, simple yet potent, makes it easy to check if a transaction date falls on a holidays, adding another layer of context to my dataset.

from datetime import date
import holidays
 
# Creating a holiday dictionary for Germany.
de_holidays = holidays.Germany()
de_holidays = holidays.Germany(years=[2024]) #It is possible to specify a year.
 
date (2024, 1, 1) in de_holidays #true
date (2024, 1, 5) in de_holidays #true
 
# Simple check if a date is a holiday
print(date(2024, 1, 1) in de_holidays)  # True for New Year's Day
print(de_holidays.get('2024-01-01'))  # Returns "Neujahr"

Its simplicity is its strength. The library provides a dictionary of holidays for a given country, which can be easily queried.

#We can use this to check for timestamps in a transaction dataset.
timestamps = ['2024-12-25 00:00:01', '2024-12-31 23:59:59']
 
def is_holiday(timestamp):
    transaction_date = datetime.fromisoformat(timestamp).date()
    return transaction_date in de_holidays
 
# Application to a dataset.
holiday_transactions = [(timestamp, is_holiday(timestamp)) for timestamp in timestamps]

In this hypothetical script, each transaction's timestamp is parsed and checked against the de_holidays dictionary. The result is a simple boolean flag that is true if the transaction date is a public holiday.

in this hyoothetical script, each transaction's timestamp is parsed and checked against the de_holidays dictionary the result is a simple boolean flag that's true if the transaction date is a public holidays

The bigger picture.

By integrating holiday awareness, we're essentially adding another dimension to the data, a tactic that fits neatly into my philosophy of "more data, more insights." The process is ripe for automation, and can be easily integrated into the data pipeline.

Yet, it is not a silver bullet. Holidays can be complex and vary widely from country to country. The holidays package is expansive but not exhaustive, and there's a need for nuance - understanding local observances, unexpected non-working days, and the like.

# Adding custom holidays or checking multiple countries.
de_holidays.append({"2024-02-14": "Valentine's Day"})
print(date(2024, 2, 14) in de_holidays)  # This will now return True for Valentine's Day

Integrating this library is a promising stride towards pinpointing fraud amids the holiday chaos. This attempt - while exploratory - seems to hold promise based on historical data.

The hope is that, by weaving in the holiday data, I edge closer to a more robust fraud detection system. The key to success often lies in the details, and this is a detail worth exploring.