Skip to content

openaq

OpenAQ Reader

OPENAQ

Legacy OPENAQ class.

Source code in monetio/readers/openaq.py
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
class OPENAQ:
    """Legacy OPENAQ class."""

    NON_MOLEC_PARAMS = ["pm1", "pm25", "pm4", "pm10", "bc"]
    PPM_TO_UGM3 = {
        "o3": 1990,
        "co": 1160,
        "no2": 1900,
        "no": 1240,
        "so2": 2650,
        "ch4": 664,
        "co2": 1820,
    }
    PPM_TO_UGM3["nox"] = PPM_TO_UGM3["no2"]

    def __init__(self, engine: str = "pandas"):
        self.engine = engine

    def build_urls(self, dates: pd.DatetimeIndex | list[datetime] | datetime | str) -> list[str]:
        return build_urls(dates)

    def add_data(
        self,
        dates: pd.DatetimeIndex | list[datetime] | datetime | str,
        *,
        num_workers: int = 1,
        wide_fmt: bool = True,
        lazy: bool = False,
    ) -> pd.DataFrame | xr.Dataset:
        reader = OpenAQReader()
        # num_workers is ignored in modern reader as it relies on dask config
        return reader.open_dataset(dates=dates, wide_fmt=wide_fmt, lazy=lazy, as_xarray=False)

OpenAQReader

Bases: PointReader

OpenAQ Reader for real-time fetches (JSONL format).

Source code in monetio/readers/openaq.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
@register_reader("openaq")
class OpenAQReader(PointReader):
    """
    OpenAQ Reader for real-time fetches (JSONL format).
    """

    def open_dataset(
        self,
        files: str | list[str] = None,
        dates: pd.DatetimeIndex | list[datetime] | datetime | str = None,
        wide_fmt: bool = True,
        as_xarray: bool = True,
        lazy: bool = False,
        **kwargs: Any,
    ) -> Union[pd.DataFrame, xr.Dataset, "dd.DataFrame"]:
        """
        Retrieve and load OpenAQ data.

        Parameters
        ----------
        files : Union[str, List[str]], optional
            File path, list of paths, or glob pattern.
        dates : Union[pd.DatetimeIndex, List[datetime], datetime, str], optional
            Dates to retrieve if files are not provided.
        wide_fmt : bool, optional
            Whether to return data in wide format, by default True.
        as_xarray : bool, optional
            Whether to return an xarray.Dataset, by default True.
        lazy : bool, optional
            Whether to return a dask-backed object, by default False.
        **kwargs : dict
            Additional arguments.

        Returns
        -------
        Union[pd.DataFrame, xr.Dataset, dd.DataFrame]
            The loaded OpenAQ data.

        Examples
        --------
        >>> from monetio.readers.openaq import OpenAQReader
        >>> reader = OpenAQReader()
        >>> ds = reader.open_dataset(dates='2023-01-01', lazy=True)
        """

        # For backward compatibility, if the first argument looks like dates, swap them.
        if (
            files is not None
            and dates is None
            and isinstance(files, pd.DatetimeIndex | datetime | pd.Timestamp | list | str)
        ):
            # If it's a string or list, it could be files.
            # But if it's a DatetimeIndex or a single datetime, it's definitely dates.
            if isinstance(files, pd.DatetimeIndex | datetime | pd.Timestamp):
                dates = files
                files = None
            elif isinstance(files, list) and len(files) > 0 and isinstance(files[0], datetime):
                dates = files
                files = None

        if files is None and dates is not None:
            files = build_urls(dates)

        # Use a more robust check for files
        has_files = files is not None
        if has_files:
            if isinstance(files, list | pd.Series | np.ndarray):
                has_files = len(files) > 0
            elif isinstance(files, str):
                has_files = True
            else:
                try:
                    has_files = bool(files)
                except ValueError:
                    has_files = len(files) > 0

        if not has_files:
            # Return empty object of correct type
            if lazy:
                import dask.dataframe as dd

                df = dd.from_pandas(pd.DataFrame(), npartitions=1)
            else:
                df = pd.DataFrame()

            if as_xarray:
                return xr.Dataset()
            return df

        # Use base class to open
        df = super().open_dataset(
            files,
            read_method=read_openaq_json,
            as_xarray=False,
            lazy=lazy,
            **kwargs,
        )

        # Post-processing (always long format to maintain laziness)
        df = self._post_process(df, dates=dates)
        df = self.harmonize(df)

        if as_xarray:
            # Pop expand2d from kwargs if present to avoid multiple values error
            exp2d = kwargs.pop("expand2d", wide_fmt)
            ds = self.to_xarray(df, expand2d=exp2d, **kwargs)

            # Update history
            ds = update_history(ds, "Read OpenAQ data.")
            return ds

        if wide_fmt:
            from ..util import long_to_wide

            df = long_to_wide(df)

        return df

    def _post_process(
        self,
        df: Union[pd.DataFrame, "dd.DataFrame"],
        dates: pd.DatetimeIndex | list[datetime] | datetime | str = None,
    ) -> Union[pd.DataFrame, "dd.DataFrame"]:
        """
        Internal post-processing logic (backend-agnostic).

        Parameters
        ----------
        df : Union[pd.DataFrame, dd.DataFrame]
            Input dataframe.
        dates : Union[pd.DatetimeIndex, List[datetime], datetime, str], optional
            Requested dates for filtering.

        Returns
        -------
        Union[pd.DataFrame, dd.DataFrame]
            The post-processed dataframe.
        """
        # Determine backend
        try:
            import dask.dataframe as dd

            is_dask = isinstance(df, dd.DataFrame)
        except ImportError:
            is_dask = False

        # Use .empty for Pandas and .npartitions for Dask
        if is_dask:
            if df.npartitions == 0:
                return df
        else:
            if df.empty:
                return df

        if dates is not None:
            dates = pd.DatetimeIndex(np.atleast_1d(pd.to_datetime(dates)))
            df = df.loc[(df.time >= dates.min()) & (df.time <= dates.max())]

        # 1. SITE ID (Lazy friendly)
        def _get_siteid(df_part: pd.DataFrame) -> pd.DataFrame:
            if df_part.empty:
                df_part["siteid"] = pd.Series(dtype=object)
                return df_part

            # to_hash might be missing some columns if the file was empty or different
            needed = ["location", "latitude", "longitude"]
            for col in needed:
                if col not in df_part.columns:
                    df_part["siteid"] = "unknown"
                    return df_part

            to_hash = (
                df_part.location.astype(str)
                + " "
                + df_part.latitude.astype(str)
                + " "
                + df_part.longitude.astype(str)
            )
            # Use country + hash
            country = df_part.country if "country" in df_part.columns else "XX"
            df_part["siteid"] = (
                country.astype(str)
                + "_"
                + to_hash.str.encode("utf-8")
                .apply(lambda b: hashlib.sha1(b).hexdigest() if pd.notnull(b) else "nan")
                .str.slice(0, 7)
            )
            return df_part

        if is_dask:
            df = df.map_partitions(_get_siteid)
        else:
            df = _get_siteid(df)

        # Rename parameter/unit to variable/units for MONETIO consistency
        df = df.rename(columns={"parameter": "variable", "unit": "units", "value": "obs"})

        # 2. Unit Conversions (Lazy friendly)
        ppm_to_ugm3 = {
            "o3": 1990,
            "co": 1160,
            "no2": 1900,
            "no": 1240,
            "so2": 2650,
            "ch4": 664,
            "co2": 1820,
        }
        ppm_to_ugm3["nox"] = ppm_to_ugm3["no2"]

        def _convert_units(df_part: pd.DataFrame) -> pd.DataFrame:
            if df_part.empty:
                return df_part
            for vn, f in ppm_to_ugm3.items():
                if "variable" in df_part.columns and "units" in df_part.columns:
                    is_ug = (df_part.variable == vn) & (df_part.units == "µg/m³")
                    df_part.loc[is_ug, "obs"] /= f
                    df_part.loc[is_ug, "units"] = "ppm"
            return df_part

        if is_dask:
            df = df.map_partitions(_convert_units)
        else:
            df = _convert_units(df)

        # Drop duplicates consistently for both paths to ensure reliable 2D expansion
        subset = [
            "time",
            "latitude",
            "longitude",
            "siteid",
            "variable",
        ]
        subset = [c for c in subset if c in df.columns]
        df = df.drop_duplicates(subset=subset)

        # Update history if attributes exist
        df = update_history(df, "Post-processed OpenAQ data (siteid, unit conversion).")

        return df

    def to_xarray(
        self, df: Union[pd.DataFrame, "dd.DataFrame"], expand2d: bool = True, **kwargs: Any
    ) -> xr.Dataset:
        """
        Convert OpenAQ DataFrame to Xarray Dataset, ensuring consistent naming.

        Parameters
        ----------
        df : Union[pd.DataFrame, dd.DataFrame]
            Input dataframe.
        expand2d : bool, optional
            Whether to expand to 2D (time, node) structure, by default True.
        **kwargs : dict
            Additional arguments passed to super().to_xarray.

        Returns
        -------
        xr.Dataset
            The loaded dataset.
        """
        ds = super().to_xarray(df, expand2d=expand2d, **kwargs)

        if expand2d:
            # If it was expanded via ds_to_2d, it will have o3, pm25 etc. as data vars
            # and o3_unit, pm25_unit etc. as well.
            # We want to rename them to o3_ppm, pm25_ugm3 to match consistent MONETIO naming.
            ppm_vars = ["o3", "co", "no2", "no", "so2", "ch4", "co2", "nox"]
            ugm3_vars = ["pm1", "pm25", "pm4", "pm10", "bc"]

            rename_dict = {}
            for v in ppm_vars:
                if v in ds.data_vars:
                    ds[v].attrs["units"] = "ppm"
                    rename_dict[v] = f"{v}_ppm"
                    if f"{v}_unit" in ds.data_vars:
                        ds = ds.drop_vars(f"{v}_unit")
            for v in ugm3_vars:
                if v in ds.data_vars:
                    ds[v].attrs["units"] = "ug/m3"
                    rename_dict[v] = f"{v}_ugm3"
                    if f"{v}_unit" in ds.data_vars:
                        ds = ds.drop_vars(f"{v}_unit")

            if rename_dict:
                ds = ds.rename(rename_dict)
                # Format units to LaTeX if applicable
                from .base import _format_units

                ds = _format_units(ds)
                ds = update_history(ds, f"Renamed variables: {list(rename_dict.values())}")

        return ds

open_dataset(files=None, dates=None, wide_fmt=True, as_xarray=True, lazy=False, **kwargs)

Retrieve and load OpenAQ data.

Parameters:

Name Type Description Default
files Union[str, List[str]]

File path, list of paths, or glob pattern.

None
dates Union[DatetimeIndex, List[datetime], datetime, str]

Dates to retrieve if files are not provided.

None
wide_fmt bool

Whether to return data in wide format, by default True.

True
as_xarray bool

Whether to return an xarray.Dataset, by default True.

True
lazy bool

Whether to return a dask-backed object, by default False.

False
**kwargs dict

Additional arguments.

{}

Returns:

Type Description
Union[DataFrame, Dataset, DataFrame]

The loaded OpenAQ data.

Examples:

>>> from monetio.readers.openaq import OpenAQReader
>>> reader = OpenAQReader()
>>> ds = reader.open_dataset(dates='2023-01-01', lazy=True)
Source code in monetio/readers/openaq.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def open_dataset(
    self,
    files: str | list[str] = None,
    dates: pd.DatetimeIndex | list[datetime] | datetime | str = None,
    wide_fmt: bool = True,
    as_xarray: bool = True,
    lazy: bool = False,
    **kwargs: Any,
) -> Union[pd.DataFrame, xr.Dataset, "dd.DataFrame"]:
    """
    Retrieve and load OpenAQ data.

    Parameters
    ----------
    files : Union[str, List[str]], optional
        File path, list of paths, or glob pattern.
    dates : Union[pd.DatetimeIndex, List[datetime], datetime, str], optional
        Dates to retrieve if files are not provided.
    wide_fmt : bool, optional
        Whether to return data in wide format, by default True.
    as_xarray : bool, optional
        Whether to return an xarray.Dataset, by default True.
    lazy : bool, optional
        Whether to return a dask-backed object, by default False.
    **kwargs : dict
        Additional arguments.

    Returns
    -------
    Union[pd.DataFrame, xr.Dataset, dd.DataFrame]
        The loaded OpenAQ data.

    Examples
    --------
    >>> from monetio.readers.openaq import OpenAQReader
    >>> reader = OpenAQReader()
    >>> ds = reader.open_dataset(dates='2023-01-01', lazy=True)
    """

    # For backward compatibility, if the first argument looks like dates, swap them.
    if (
        files is not None
        and dates is None
        and isinstance(files, pd.DatetimeIndex | datetime | pd.Timestamp | list | str)
    ):
        # If it's a string or list, it could be files.
        # But if it's a DatetimeIndex or a single datetime, it's definitely dates.
        if isinstance(files, pd.DatetimeIndex | datetime | pd.Timestamp):
            dates = files
            files = None
        elif isinstance(files, list) and len(files) > 0 and isinstance(files[0], datetime):
            dates = files
            files = None

    if files is None and dates is not None:
        files = build_urls(dates)

    # Use a more robust check for files
    has_files = files is not None
    if has_files:
        if isinstance(files, list | pd.Series | np.ndarray):
            has_files = len(files) > 0
        elif isinstance(files, str):
            has_files = True
        else:
            try:
                has_files = bool(files)
            except ValueError:
                has_files = len(files) > 0

    if not has_files:
        # Return empty object of correct type
        if lazy:
            import dask.dataframe as dd

            df = dd.from_pandas(pd.DataFrame(), npartitions=1)
        else:
            df = pd.DataFrame()

        if as_xarray:
            return xr.Dataset()
        return df

    # Use base class to open
    df = super().open_dataset(
        files,
        read_method=read_openaq_json,
        as_xarray=False,
        lazy=lazy,
        **kwargs,
    )

    # Post-processing (always long format to maintain laziness)
    df = self._post_process(df, dates=dates)
    df = self.harmonize(df)

    if as_xarray:
        # Pop expand2d from kwargs if present to avoid multiple values error
        exp2d = kwargs.pop("expand2d", wide_fmt)
        ds = self.to_xarray(df, expand2d=exp2d, **kwargs)

        # Update history
        ds = update_history(ds, "Read OpenAQ data.")
        return ds

    if wide_fmt:
        from ..util import long_to_wide

        df = long_to_wide(df)

    return df

to_xarray(df, expand2d=True, **kwargs)

Convert OpenAQ DataFrame to Xarray Dataset, ensuring consistent naming.

Parameters:

Name Type Description Default
df Union[DataFrame, DataFrame]

Input dataframe.

required
expand2d bool

Whether to expand to 2D (time, node) structure, by default True.

True
**kwargs dict

Additional arguments passed to super().to_xarray.

{}

Returns:

Type Description
Dataset

The loaded dataset.

Source code in monetio/readers/openaq.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def to_xarray(
    self, df: Union[pd.DataFrame, "dd.DataFrame"], expand2d: bool = True, **kwargs: Any
) -> xr.Dataset:
    """
    Convert OpenAQ DataFrame to Xarray Dataset, ensuring consistent naming.

    Parameters
    ----------
    df : Union[pd.DataFrame, dd.DataFrame]
        Input dataframe.
    expand2d : bool, optional
        Whether to expand to 2D (time, node) structure, by default True.
    **kwargs : dict
        Additional arguments passed to super().to_xarray.

    Returns
    -------
    xr.Dataset
        The loaded dataset.
    """
    ds = super().to_xarray(df, expand2d=expand2d, **kwargs)

    if expand2d:
        # If it was expanded via ds_to_2d, it will have o3, pm25 etc. as data vars
        # and o3_unit, pm25_unit etc. as well.
        # We want to rename them to o3_ppm, pm25_ugm3 to match consistent MONETIO naming.
        ppm_vars = ["o3", "co", "no2", "no", "so2", "ch4", "co2", "nox"]
        ugm3_vars = ["pm1", "pm25", "pm4", "pm10", "bc"]

        rename_dict = {}
        for v in ppm_vars:
            if v in ds.data_vars:
                ds[v].attrs["units"] = "ppm"
                rename_dict[v] = f"{v}_ppm"
                if f"{v}_unit" in ds.data_vars:
                    ds = ds.drop_vars(f"{v}_unit")
        for v in ugm3_vars:
            if v in ds.data_vars:
                ds[v].attrs["units"] = "ug/m3"
                rename_dict[v] = f"{v}_ugm3"
                if f"{v}_unit" in ds.data_vars:
                    ds = ds.drop_vars(f"{v}_unit")

        if rename_dict:
            ds = ds.rename(rename_dict)
            # Format units to LaTeX if applicable
            from .base import _format_units

            ds = _format_units(ds)
            ds = update_history(ds, f"Renamed variables: {list(rename_dict.values())}")

    return ds

build_urls(dates)

Construct OpenAQ S3 URLs for the given dates.

Parameters:

Name Type Description Default
dates Union[DatetimeIndex, List[datetime], datetime, str]

Dates to build URLs for.

required

Returns:

Type Description
List[str]

List of S3 URLs.

Source code in monetio/readers/openaq.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
def build_urls(dates: pd.DatetimeIndex | list[datetime] | datetime | str) -> list[str]:
    """
    Construct OpenAQ S3 URLs for the given dates.

    Parameters
    ----------
    dates : Union[pd.DatetimeIndex, List[datetime], datetime, str]
        Dates to build URLs for.

    Returns
    -------
    List[str]
        List of S3 URLs.
    """
    import s3fs

    fs = s3fs.S3FileSystem(anon=True)
    s3bucket = "openaq-fetches/realtime"

    dates = pd.to_datetime(dates)
    if isinstance(dates, pd.Timestamp):
        dates = pd.DatetimeIndex([dates])
    dates = dates.floor("D").unique()

    # Get available days from S3
    try:
        folders = fs.ls(s3bucket)
    except Exception as e:
        logger.error(f"Failed to list S3 bucket {s3bucket}: {e}")
        raise

    days_available = [folder.split("/")[-1] for folder in folders]
    dates_available = pd.to_datetime(days_available, format=r"%Y-%m-%d", errors="coerce")

    dates_requested = pd.Series(dates).floor("D").drop_duplicates()
    dates_have = dates_requested[dates_requested.isin(dates_available)]

    urls = []
    for date in dates_have:
        sdate = date.strftime(r"%Y-%m-%d")
        try:
            files = fs.ls(f"{s3bucket}/{sdate}")
            urls.extend(f"s3://{f}" for f in files)
        except Exception as e:
            logger.warning(f"Failed to list files for date {sdate}: {e}")

    return urls

read_json(fp_or_url, **kwargs)

Legacy wrapper for read_openaq_json.

Source code in monetio/readers/openaq.py
481
482
483
def read_json(fp_or_url: str, **kwargs: Any) -> pd.DataFrame:
    """Legacy wrapper for read_openaq_json."""
    return read_openaq_json(fp_or_url, **kwargs)

read_json2(fp_or_url, **kwargs)

Legacy wrapper for read_openaq_json.

Source code in monetio/readers/openaq.py
486
487
488
489
def read_json2(fp_or_url: str, **kwargs: Any) -> pd.DataFrame:
    """Legacy wrapper for read_openaq_json."""
    # Note: original read_json2 used requests, but read_openaq_json is preferred.
    return read_openaq_json(fp_or_url, **kwargs)

read_openaq_json(fn, storage_options=None, **kwargs)

Read an OpenAQ JSONL file.

Parameters:

Name Type Description Default
fn str

File path or URL.

required
storage_options dict

Storage options for fsspec, by default None.

None
**kwargs Any

Additional arguments.

{}

Returns:

Type Description
DataFrame

The loaded data.

Source code in monetio/readers/openaq.py
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
def read_openaq_json(fn: str, storage_options: dict = None, **kwargs: Any) -> pd.DataFrame:
    """
    Read an OpenAQ JSONL file.

    Parameters
    ----------
    fn : str
        File path or URL.
    storage_options : dict, optional
        Storage options for fsspec, by default None.
    **kwargs : Any
        Additional arguments.

    Returns
    -------
    pd.DataFrame
        The loaded data.
    """
    # Convert HTTP URLs to s3:// if they are in the openaq-fetches bucket
    # to avoid 403 Forbidden issues in some environments.
    if isinstance(fn, str) and "openaq-fetches.s3.amazonaws.com" in fn:
        fn = fn.replace("https://openaq-fetches.s3.amazonaws.com", "s3://openaq-fetches")
        fn = fn.replace("http://openaq-fetches.s3.amazonaws.com", "s3://openaq-fetches")
        if storage_options is None:
            storage_options = {"anon": True}

    try:
        df = pd.read_json(fn, lines=True, storage_options=storage_options)
    except Exception as e:
        logger.debug(f"Failed to read OpenAQ JSON {fn}: {e}")
        raise

    if df.empty:
        return df

    if "attribution" in df.columns:
        df = df.drop(columns="attribution")

    if "coordinates" not in df.columns:
        return pd.DataFrame()

    df = df.dropna(subset=["coordinates"])
    if df.empty:
        return df

    to_expand = ["date", "averagingPeriod", "coordinates"]
    to_expand = [c for c in to_expand if c in df.columns]

    # Expand JSON columns
    new = pd.json_normalize(json.loads(df[to_expand].to_json(orient="records")))

    # Process Time
    if "date.utc" in new.columns:
        time = pd.to_datetime(new["date.utc"]).dt.tz_localize(None)
    else:
        time = pd.Series(np.nan, index=new.index, dtype="datetime64[ns]")

    if "date.local" in new.columns:
        try:
            # Handle possible varied offset formats like +0100 or +01:00
            utcoffset_str = new["date.local"].str.slice(-6, None)
            # Replace +0100 with +01:00 if necessary
            utcoffset_str = utcoffset_str.str.replace(r"(\d{2})(\d{2})$", r"\1:\2", regex=True)
            utcoffset = pd.to_timedelta(utcoffset_str)
        except Exception:
            utcoffset = pd.Timedelta(0)
    else:
        utcoffset = pd.Timedelta(0)

    time_local = time + utcoffset

    # Averaging period
    averagingPeriod = pd.Series(np.full(len(new), np.nan, dtype="timedelta64[ns]"))
    if "averagingPeriod.value" in new.columns and "averagingPeriod.unit" in new.columns:
        value = new["averagingPeriod.value"]
        units = new["averagingPeriod.unit"]
        unique_units = units.dropna().unique()
        for unit in unique_units:
            is_unit = units == unit
            try:
                # Map units for pd.to_timedelta
                u = unit
                if u == "hours":
                    u = "h"
                elif u == "minutes":
                    u = "m"
                elif u == "seconds":
                    u = "s"
                averagingPeriod.loc[is_unit] = pd.to_timedelta(value[is_unit], unit=u)
            except Exception:
                pass

    # Reassemble DataFrame
    df = df.drop(columns=to_expand).assign(
        time=time,
        time_local=time_local,
        utcoffset=utcoffset,
        averagingPeriod=averagingPeriod,
    )

    if "coordinates.latitude" in new.columns:
        df["latitude"] = new["coordinates.latitude"]
    if "coordinates.longitude" in new.columns:
        df["longitude"] = new["coordinates.longitude"]

    if "value" in df.columns:
        df["value"] = df["value"].astype(float)

    return df