Set Time Range Values to Constant

Replace measurements in specific time periods with a constant value. Useful for removing instrumental errors or correcting known malfunction periods.

Create hourly time series

Generate sample data spanning 48 hours with hourly frequency. Values range from 1-48, representing measurements over two days.

import pandas as pd
import diive as dv

dates = pd.date_range('2022-06-01', periods=48, freq='h')
values = range(1, 49)
series = pd.Series(values, index=dates, name='flux')

print("Original data:")
print(f"  Shape: {series.shape}")
print(f"  Range: {series.min()} to {series.max()}")
print(f"  First 12 hours:\n{series.head(12)}")
Original data:
  Shape: (48,)
  Range: 1 to 48
  First 12 hours:
2022-06-01 00:00:00     1
2022-06-01 01:00:00     2
2022-06-01 02:00:00     3
2022-06-01 03:00:00     4
2022-06-01 04:00:00     5
2022-06-01 05:00:00     6
2022-06-01 06:00:00     7
2022-06-01 07:00:00     8
2022-06-01 08:00:00     9
2022-06-01 09:00:00    10
2022-06-01 10:00:00    11
2022-06-01 11:00:00    12
Freq: h, Name: flux, dtype: int64

Replace values in time ranges

Set a single timestamp and a date range to a constant value (0). This is useful for removing periods of sensor malfunction or known errors.

series_corr = dv.corrections.setto_value(
    series=series,
    dates=[
        '2022-06-01 01:00:00',  # Single timestamp
        ['2022-06-01 03:00:00', '2022-06-01 15:00:00']  # Date range [start, end]
    ],
    value=0,
    verbose=1
)

print("\nCorrected data:")
print(f"  Changed values: {series.shape[0] - series_corr.notna().sum() + series_corr.isna().sum()}")
print(f"  New range: {series_corr[series_corr > 0].min()} to {series_corr.max()}")
print(f"  First 20 hours (showing replacements):\n{series_corr.head(20)}")
  > Records in time range ['2022-06-01 01:00:00', ['2022-06-01 03:00:00',
'2022-06-01 15:00:00']] were set to value 0.

Corrected data:
  Changed values: 0
  New range: 1 to 48
  First 20 hours (showing replacements):
2022-06-01 00:00:00     1
2022-06-01 01:00:00     0
2022-06-01 02:00:00     3
2022-06-01 03:00:00     0
2022-06-01 04:00:00     0
2022-06-01 05:00:00     0
2022-06-01 06:00:00     0
2022-06-01 07:00:00     0
2022-06-01 08:00:00     0
2022-06-01 09:00:00     0
2022-06-01 10:00:00     0
2022-06-01 11:00:00     0
2022-06-01 12:00:00     0
2022-06-01 13:00:00     0
2022-06-01 14:00:00     0
2022-06-01 15:00:00     0
2022-06-01 16:00:00    17
2022-06-01 17:00:00    18
2022-06-01 18:00:00    19
2022-06-01 19:00:00    20
Freq: h, Name: flux, dtype: int64

Total running time of the script: (0 minutes 0.005 seconds)

Gallery generated by Sphinx-Gallery