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.1.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.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pandas_timeinterval-2025.8.1.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.1.tar.gz
Algorithm Hash digest
SHA256 b3f099382453b5e28816ab258c1d52aaeb550436ed98f0b5e7f58efdeb5a1935
MD5 e94c9a7e0b32efee9c497dcb3686d876
BLAKE2b-256 6e8933cc56ed03a78c35394c585df6fc1fa15d420d7da4329a7dd6ba85a962a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pandas_timeinterval-2025.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dede9a8f252a7f73f37dee9e9a7646c95b5f68fbbbb78f126fe56c53bd46f882
MD5 90fdcdd342b352c66e1b0a715d77e3fb
BLAKE2b-256 7e1373a98662bfa02fff86bc30a2f72d641b251a116b2eea1d967eff9e59f1d5

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