.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/times/times_frequency_detection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_times_times_frequency_detection.py: TIME RESOLUTION DETECTION: AUTO-DETECT FREQUENCY ================================================= Detect time series sampling frequency with confidence scores. Three approaches: TimestampSanitizer for full validation, direct functions for fast checks, and DetectFrequency class for details. Part of the diive library: https://github.com/holukas/diive .. GENERATED FROM PYTHON SOURCE LINES 13-15 Load example data ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 15-27 .. code-block:: Python import pandas as pd import diive as dv df = dv.load_exampledata_parquet() series = df['Tair_f'].copy() print("Example data loaded:") print(f" Records: {len(series)}") print(f" Period: {series.index.min()} to {series.index.max()}") .. rst-class:: sphx-glr-script-out .. code-block:: none > Loaded .parquet file /home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.050 seconds). Example data loaded: Records: 175296 Period: 2013-01-01 00:15:00 to 2022-12-31 23:45:00 .. GENERATED FROM PYTHON SOURCE LINES 28-33 Method 1: TimestampSanitizer (full validation) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Detects frequency as part of a 10-step validation pipeline. This gives a confidence score combining multiple detection methods. .. GENERATED FROM PYTHON SOURCE LINES 33-65 .. code-block:: Python sanitizer = dv.times.TimestampSanitizer( data=pd.DataFrame({'value': series}), regularize=False, # Skip gap-filling verbose=False ) status = sanitizer.get_status() print("\n" + "=" * 60) print("TimestampSanitizer Results") print("=" * 60) print(f"Detected frequency: {status['inferred_frequency']}") print(f"Confidence: {status['frequency_confidence']:.1%}") print(f"Detection method: {status['frequency_detection_method']}") print(f"Intervals matching: {status['frequency_percent_matching']:.1f}%") # Assess result quality confidence = status['frequency_confidence'] if confidence >= 0.95: print(f"[OK] All methods agree") elif confidence >= 0.90: print(f"[OK] Multiple methods agree") elif confidence >= 0.70: print(f"[!] Some disagreement - check for gaps or irregular intervals") else: print(f"[X] Methods disagree - data may be irregular") # Show alternatives if any if status['frequency_alternatives']: print(f"\nAlternatives detected: {status['frequency_alternatives']}") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ TimestampSanitizer Results ============================================================ Detected frequency: 30min Confidence: 100.0% Detection method: all_methods_agree Intervals matching: 100.0% [OK] All methods agree .. GENERATED FROM PYTHON SOURCE LINES 66-71 Method 2: Direct detection functions (lightweight) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Three independent functions for frequency analysis. Each method has different strengths - compare results to assess reliability. .. GENERATED FROM PYTHON SOURCE LINES 71-104 .. code-block:: Python freq_prog, info_prog = dv.times.timestamp_infer_freq_progressively(series.index) freq_full, info_full = dv.times.timestamp_infer_freq_from_fullset(series.index) freq_delta, info_delta = dv.times.timestamp_infer_freq_from_timedelta(series.index) print("\n" + "=" * 60) print("Direct Detection Functions") print("=" * 60) print(f"\n1. Progressive (fast):") print(f" Frequency: {freq_prog}") print(f" Info: {info_prog}") print(f"\n2. Full dataset (requires regular data):") print(f" Frequency: {freq_full}") print(f" Info: {info_full}") print(f"\n3. Timedelta (tolerates gaps):") print(f" Frequency: {freq_delta}") print(f" Info: {info_delta}") # Check agreement all_methods = [freq_prog, freq_full, freq_delta] unique_results = set(f for f in all_methods if f is not None) print("\nAgreement:") if len(unique_results) == 1: print(f"[OK] All agree: {list(unique_results)[0]}") elif len(unique_results) == 2: print(f"[!] Two methods agree on {list(unique_results)[0]}") else: print(f"[X] All three disagree: {unique_results}") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ Direct Detection Functions ============================================================ 1. Progressive (fast): Frequency: 30min Info: data 1000+1000 2. Full dataset (requires regular data): Frequency: 30min Info: full data 3. Timedelta (tolerates gaps): Frequency: 30min Info: 100% occurrence Agreement: [OK] All agree: 30min .. GENERATED FROM PYTHON SOURCE LINES 105-110 Method 3: DetectFrequency class (internal API) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Lower-level class used internally by TimestampSanitizer. Shows which detection method won and details about alternatives. .. GENERATED FROM PYTHON SOURCE LINES 110-135 .. code-block:: Python detector = dv.times.DetectFrequency(index=series.index, verbose=False) print("\n" + "=" * 60) print("DetectFrequency Class") print("=" * 60) print(f"Frequency: {detector.get()}") print(f"Confidence: {detector.confidence:.1%}") print(f"Method: {detector.detection_method}") print(f"Percent matching: {detector.percent_matching}%") if detector.alternatives: print(f"Alternatives: {detector.alternatives}") # Detection method details method_info = { 'all_methods_agree': 'All three methods found same frequency (100%)', 'full_dataset': 'Pandas inference from complete data (95%)', 'timedelta': f'Most frequent interval ({detector.confidence:.0%})', 'start_end_chunks': 'Start and end intervals match (70%)', } if detector.detection_method in method_info: print(f"\n{detector.detection_method}: {method_info[detector.detection_method]}") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ DetectFrequency Class ============================================================ Frequency: 30min Confidence: 100.0% Method: all_methods_agree Percent matching: 100.0% all_methods_agree: All three methods found same frequency (100%) .. GENERATED FROM PYTHON SOURCE LINES 136-138 Validate detected frequency ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 138-153 .. code-block:: Python expected = '30min' detected = status['inferred_frequency'] print("\n" + "=" * 60) print("Validation") print("=" * 60) print(f"Expected: {expected}") print(f"Detected: {detected}") if detected == expected: print(f"[OK] Match") else: print(f"[X] Mismatch") print(f"Check for: gaps, duplicate timestamps, incorrect frequency specification") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ Validation ============================================================ Expected: 30min Detected: 30min [OK] Match .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.117 seconds) .. _sphx_glr_download_auto_examples_times_times_frequency_detection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: times_frequency_detection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: times_frequency_detection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: times_frequency_detection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_