Parsing Time with Pendulum: Simplify Your Date and Time Operations – Data Science 2026
Parsing dates and times from messy strings (logs, APIs, CSVs, user input) is one of the most frustrating yet frequent tasks in data science. The pendulum library makes this dramatically easier, more readable, and more reliable than the standard datetime module. In 2026, Pendulum remains a favorite for developers who want human-friendly, timezone-aware parsing without writing complex format strings or error-prone try/except blocks.
TL;DR — Why Use Pendulum for Parsing
- Extremely flexible
pendulum.parse()handles almost any date string - Built-in timezone support with
timezoneparameter - Clean, readable API compared to
datetime.strptime() - Perfect for real-world messy data sources
1. Basic Parsing with Pendulum
import pendulum
# Flexible parsing of almost any string
dt1 = pendulum.parse("2026-03-19")
dt2 = pendulum.parse("March 19th, 2026 3:45 PM")
dt3 = pendulum.parse("19/03/2026 15:45:30")
dt4 = pendulum.parse("3 hours ago")
print(dt1)
print(dt2)
print(dt3)
print(dt4) # relative time also supported
2. Timezone-Aware Parsing
# Parse with explicit timezone
utc_time = pendulum.parse("2026-03-19 14:30:00", tz="UTC")
ny_time = pendulum.parse("2026-03-19 14:30:00", tz="America/New_York")
print(utc_time)
print(ny_time)
# Convert between timezones
ny_time_in_utc = ny_time.in_timezone("UTC")
3. Real-World Data Science Examples
import pandas as pd
import pendulum
df = pd.read_csv("logs.csv")
# Example 1: Parse messy log timestamps
df["parsed_time"] = df["timestamp"].apply(
lambda x: pendulum.parse(x, tz="UTC") if pd.notna(x) else None
)
# Example 2: Extract components after parsing
df["hour"] = df["parsed_time"].apply(lambda x: x.hour if x else None)
df["day_of_week"] = df["parsed_time"].apply(lambda x: x.day_of_week if x else None)
# Example 3: Relative time parsing (great for logs)
df["time_ago"] = df["event_time"].apply(lambda x: pendulum.parse(x, tz="UTC"))
df["minutes_ago"] = df["time_ago"].apply(lambda x: (pendulum.now("UTC") - x).in_minutes())
4. Best Practices in 2026
- Use
pendulum.parse()for any unstructured or messy date strings - Always specify
tzparameter for timezone-aware results - Combine with pandas
.apply()for vectorized parsing on large DataFrames - Use
pendulum.now(tz=...)instead ofdatetime.now() - Fallback to standard
datetimeordateutilonly for very specific edge cases
Conclusion
Parsing time with Pendulum makes one of the most painful parts of data science work simple, reliable, and enjoyable. In 2026, its flexible parse() function, built-in timezone support, and clean API make it the preferred choice for handling logs, CSVs, APIs, and any source of messy datetime strings.
Next steps:
- Try replacing your current date parsing code with
pendulum.parse()on one messy dataset and enjoy the simplicity