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-2024.6.3.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

pandas_timeinterval-2024.6.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pandas_timeinterval-2024.6.3.tar.gz
Algorithm Hash digest
SHA256 5c63cf314bcf7e2265a1e76bdb6dcc839134f0fb02fd0c11f6535bc4e12b20a3
MD5 8a0ef267baef8809852db8fbc87650b0
BLAKE2b-256 f07846520afda5dfbe49264458dfe9d17a03a4fcb517c6e931821b510518194d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pandas_timeinterval-2024.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 88e56365b915036e4e80bc62182c1fef55fe3a3aa37dd1009f15310a3a60e383
MD5 aef7cecefeb4436cb3b178fb5f790d4a
BLAKE2b-256 eb0e2618bd3213a8e04b6feb076397383b8071916fbfd925ae803e012133bf75

See more details on using hashes here.

Supported by

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