DEV Community

Murabyte
Murabyte

Posted on Edited on

Turn raw attendance punches into one row per employee per day with pandas

Disclosure: I make PunchSheet at Murabyte, which is mentioned at the end. Written with AI assistance and reviewed by Murabyte.

Fingerprint and attendance devices usually export one line per punch. To get a monthly report you need one row per employee per day, with first in, last out and hours. This is a short pandas script that does that, and what to check before you trust it.

The input

Assume a CSV with three columns: emp_id, name, timestamp, for example 1001,Sara,2026-11-03 08:04:12. Many devices export something close to this. If yours has separate date and time columns, join them first.

Load and clean

import pandas as pd

df = pd.read_csv("punches.csv", parse_dates=["timestamp"])
df = df.sort_values(["emp_id", "timestamp"])

# drop double scans: same employee within 2 minutes of the previous punch
gap = df.groupby("emp_id")["timestamp"].diff()
df = df[(gap.isna()) | (gap > pd.Timedelta(minutes=2))]
Enter fullscreen mode Exit fullscreen mode

Double scans are common when someone presses the sensor twice. The 2 minute rule is a choice. Write yours down so the report is repeatable.

One row per employee per day

df["day"] = df["timestamp"].dt.date
daily = (df.groupby(["emp_id", "name", "day"])["timestamp"]
           .agg(first_in="min", last_out="max", punches="count")
           .reset_index())
daily["hours"] = ((daily["last_out"] - daily["first_in"])
                  .dt.total_seconds() / 3600).round(2)
Enter fullscreen mode Exit fullscreen mode

Flag the problem days

daily["check"] = daily["punches"].eq(1)  # one punch only: missing in or out
daily.to_excel("attendance_daily.xlsx", index=False)
Enter fullscreen mode Exit fullscreen mode

A day with one punch has no real out time. Do not count it as zero hours; mark it and decide with the manager.

Night shifts

Grouping by calendar date breaks shifts that cross midnight. A simple fix is to shift the clock back by the shift length before taking the date. For a shift that starts at 22:00, subtract 6 hours, so 22:00 to 06:00 stays on one "work day":

df["day"] = (df["timestamp"] - pd.Timedelta(hours=6)).dt.date
Enter fullscreen mode Exit fullscreen mode

Only use this for employees on that shift, and keep the original timestamps for the first and last punch.

Check one employee by hand

Pick one employee and compare their month with the raw punches. If the first and last punch per day match, the rules work for everyone.

When you do this every month

Maintaining the script gets tedious: more devices, different file formats, shifts, grace periods, Friday off. PunchSheet is a Windows app that does this offline. It reads device exports (CSV, TXT, DAT, XLS, XLSX), applies shifts, and exports colored daily and monthly Excel reports. It is $49 one time and the license key works on 2 PCs. The free trial opens your files and shows the on-screen preview; exporting the Excel report needs the license key.

More on the problem: https://murabyte.netlify.app/en/attendance-device-export-to-excel
Free trial: https://murabyte.gumroad.com/l/punchsheet-trial
Gumroad: https://murabyte.gumroad.com/l/punchsheet

Microsoft Store: https://apps.microsoft.com/detail/9N8MT2L4CSDS

python #pandas #excel #hr

Top comments (1)

Collapse
 
ahmetozel profile image
Ahmet Özel •

Documenting the two-minute double-scan dedup threshold explicitly and flagging single-punch days for human review instead of silently logging zero hours are exactly the safeguards that prevent payroll disputes. Shifting the timestamp back by six hours strictly for night-shift workers before extracting the calendar date while preserving raw first_in and last_out timestamps is also a clean, readable pandas pattern.

One edge case worth adding as files grow is handling timezone-aware timestamps across daylight saving transitions, where naive subtraction of last_out minus first_in can be off by an hour unless converted to UTC first.