Night shifts in attendance exports: grouping punches into shifts that cross midnight in pandas
Night shifts in attendance exports: grouping punches into shifts that cross midnight in pandas
A night shift breaks the usual assumption behind every attendance script: that a punch belongs to the calendar day it was recorded on. For someone working 22:00 to 06:00, the check-in is on one date and the check-out on the next. Group by date and you get two broken rows per shift.
This is a small pandas recipe that fixes it, and the checks that tell you it worked.
The data you get from the device
A typical export has one row per punch:
employee_id,punch_time,device 101,2026-11-24 21:57:00,gate-1 101,2026-11-25 06:03:00,gate-1 Two punches, two dates, one shift. If you pivot on the date, day 24 has a check-in and no check-out, and day 25 has a check-out and no check-in. Both rows look like a missing punch, and every rule you built on top of them fires wrongly.
Give each shift a business date
The fix is to attach every punch to the shift it belongs to, not to the date it happened. A shift has a start time and a length. Anything punched before the shift's cutoff belongs to the previous business day.
import pandas as pd SHIFT_START = pd.Timedelta(hours=22) CUTOFF = pd.Timedelta(hours=6) # punches before 06:00 belong to yesterday df = pd.read_csv("punches.csv", parse_dates=["punch_time"]) df["business_date"] = (df["punch_time"] - CUTOFF).dt.normalize() Subtracting the cutoff moves early-morning punches back into the previous date. 06:03 minus 6 hours is 00:03 on the 25th... that is still the 25th, so use a cutoff slightly after the shift end instead, for example 07:00, so 06:03 lands on the 24th.
Pick the cutoff as "the latest time a night shift can end", not as the shift start.
Then take first in and last out per business date
g = df.groupby(["employee_id", "business_date"])["punch_time"] shift = pd.DataFrame({ "check_in": g.min(), "check_out": g.max(), "punches": g.count(), }).reset_index() shift["hours"] = (shift["check_out"] - shift["check_in"]).dt.total_seconds() / 3600 Now one row is one shift, and the hours are the real elapsed time, not two half rows.
The three checks before you trust it
- Shifts with
punchesequal to 1 are missing punches, not zero-hour days. List them separately instead of counting them as absence. - Any shift longer than your longest allowed shift is either a forgotten check-out or two shifts merged. Both need a human look.
- The sum of hours for the month should match the device's own monthly report within a few minutes. If it is off by whole shifts, the cutoff is wrong.
Day shifts and night shifts in the same file
If your staff mix both, run the grouping per employee after tagging each person's shift, or use one cutoff that is later than every shift end in the company. The second is simpler and works when nobody starts before the cutoff.
When the spreadsheet is the bottleneck
Doing this in pandas is fine if you already work in Python. If the person who needs the report is in HR, PunchSheet does the same grouping in a Windows app: it reads the device export (CSV, TXT, DAT, XLS or XLSX), applies each shift including ones that cross midnight, merges double punches, and writes daily, monthly and per-employee Excel reports. It runs offline, the license works on two PCs, and it is $49 once rather than a subscription.
Free trial: https://murabyte.gumroad.com/l/punchsheet-trial
Gumroad: https://murabyte.gumroad.com/l/punchsheet
Microsoft Store: https://apps.microsoft.com/detail/9N8MT2L4CSDS