Skip to main content

A package to trim datetime indexed DataFrames and Series in pandas.

Project description

pandas-timeinterval

A package to trim datetime-indexed DataFrames and Series in pandas. This package is useful for analyzing sparse DataFrames. It provides functionalities to generate Intervals objects using boolean Series, modify these Intervals using various methods (such as expand, contract), and combine them via set operations. The resulting Intervals can be used to trim any DataFrame using the .trim method.

Features

  • Union and Intersection: Perform union and intersection operations on interval objects.
  • Trim: Trim DataFrame/Series to only include data within specified intervals.
  • Adjust: Adjust intervals by a specified timedelta.
  • Normalize: Merge overlapping intervals into a single interval.
  • Flexible Interval Creation: Create intervals from boolean Series.

Installation

pip install pandas-timeinterval

Usage

Union and Intersection Example

import pandas as pd
from pandas_timeinterval import Intervals

# Create two Intervals objects with datetime ranges
intervals1 = Intervals([(pd.Timestamp("2024-01-01"), pd.Timestamp("2024-01-02"))])
intervals2 = Intervals([(pd.Timestamp("2024-01-03"), pd.Timestamp("2024-01-04"))])

# Perform union operation
union_intervals = intervals1.union(intervals2)
print(union_intervals)

# Perform intersection operation
intersection_intervals = intervals1.intersection(intervals2)
print(intersection_intervals)

Trim Example

import pandas as pd
from pandas_timeinterval import Intervals, Interval

# Create a sample DataFrame
sample_data = pd.Series(
    [1, 2, 3, 4, 5], index=pd.date_range(start="2021-01-01", periods=5, freq="D")
)

# Create an Intervals object
intervals = Intervals([(pd.Timestamp("2021-01-02"), pd.Timestamp("2021-01-03"))])

# Trim the data
trimmed_data = intervals.trim(sample_data)

print(trimmed_data)

Adjust Example

import pandas as pd
from pandas_timeinterval import Intervals, Interval

# Create Intervals object
intervals = Intervals(
    [Interval(pd.Timestamp("2021-01-01"), pd.Timestamp("2021-01-03")),
     Interval(pd.Timestamp("2021-01-02"), pd.Timestamp("2021-01-04"))]
)

# Adjust intervals by adding one day
adjusted_intervals = intervals.adjust(pd.Timedelta(days=1))
print(adjusted_intervals)

# Normalize intervals to merge overlapping intervals
normalized_intervals = intervals.normalize()
print(normalized_intervals)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas_timeinterval-2025.8.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandas_timeinterval-2025.8.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file pandas_timeinterval-2025.8.0.tar.gz.

File metadata

  • Download URL: pandas_timeinterval-2025.8.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pandas_timeinterval-2025.8.0.tar.gz
Algorithm Hash digest
SHA256 524e02af743aa56282cd1ca162d594735ffa260bbaae7287ec852d4df731fcbc
MD5 588f0e778de95957a0462b515409d085
BLAKE2b-256 9a6dec5c48b9874ed167cf90e5e8431c867421e87d2b85ef8250c9a9f876cb89

See more details on using hashes here.

File details

Details for the file pandas_timeinterval-2025.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pandas_timeinterval-2025.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 121fc7abb5d746e813de7999f62527bc7a9ccea4c3237cb46eb94356faf0ca45
MD5 2964e8fa4740876da8c34ca74d82c91f
BLAKE2b-256 53bbe0b6f8d502770afcec436c7fed17048239befe3173c125b4d6800ab236fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page