Skip to main content
Version: 0.0.98

ts_shape.loader.timeseries.databricks_spark_parquet_loader

Spark-native Databricks loader for the canonical hourly parquet layout.

Unlike :class:DatabricksUnityParquetLoader -- which reads a FUSE-mounted Unity Catalog Volume directly with pandas.read_parquet -- this loader uses the cluster's Spark session to read the same <base>/YYYY/MM/DD/HH/<uuid>.parquet layout. Use it when you want Spark to do the scan (predicate/column pushdown, distributed read) and only collect to pandas at the end, e.g. inside a Databricks notebook or job.

It generalizes the common notebook idiom::

hours = pd.date_range(start, end, freq="h")
paths = [f"{base}/{h:%Y/%m/%d/%H}/{uuid}.parquet" for h in hours]
sdf = spark.read.option("basePath", base).parquet(*paths)
df = sdf.select("systime", "uuid", "value_integer").toPandas()

into a reusable, parameterized loader: many UUIDs, configurable hour layout, column projection, a Spark filter expression, optional missing-path tolerance, and a choice of Spark or pandas output. The path-building step is a pure, Spark-free method so it can be inspected and unit-tested on its own.

Classes
NameDescription
DatabricksSparkParquetLoaderRead the canonical hourly parquet layout through a Spark session.

DatabricksSparkParquetLoader​

classsource
DatabricksSparkParquetLoader(
base_path: str,
spark: Any | None = None,
*,
hour_pattern: str = "%Y/%m/%d/%H",
file_template: str = "{uuid}.parquet"
) -> None

Read the canonical hourly parquet layout through a Spark session.

Parameters
NameTypeDescriptionDefault
base_pathstrRoot under which the hour folders live, e.g. /Volumes/main/plant/timeseries or abfss://.../timeseries.required
sparkAny | NoneThe SparkSession to use. When omitted, the active session is used (SparkSession.getActiveSession()); a clear :class:~ts_shape.errors.LoaderError is raised at read time if none is available.None
hour_patternstrstrftime pattern for the hour subfolder. Default "%Y/%m/%d/%H" matches the rest of ts-shape.'%Y/%m/%d/%H'
file_templatestrPer-file name template; {uuid} is substituted. Default "{uuid}.parquet".'{uuid}.parquet'
Functions
NameDescription
build_pathsBuild the explicit parquet paths covering [start, end] per UUID.
load_by_time_range_and_uuidsRead the hourly parquet files for uuids within [start, end].
fetch_data_as_dataframePandas-returning convenience for Pipeline / DataIntegratorHybrid.

build_paths​

methodsource
build_paths(
start: str | pd.Timestamp,
end: str | pd.Timestamp,
uuids: str | Sequence[str],
*,
freq: str = "h"
) -> list[str]

Build the explicit parquet paths covering [start, end] per UUID.

This is the reusable, testable core: it mirrors what Spark will read, with one path per (hour, uuid) pair. Hours are inclusive of both ends: the start is floored to the folder granularity, so 09:30 .. 11:00 reads the 09, 10 and 11 folders. The folders are in UTC: tz-aware bounds are converted to UTC, naive bounds are taken as UTC.

Parameters
NameTypeDescriptionDefault
startstr | TimestampWindow start (parsed by pandas.Timestamp).required
endstr | TimestampWindow end (inclusive).required
uuidsstr | Sequence[str]A single UUID or a sequence of UUIDs.required
freqstrFolder granularity; defaults to hourly ("h").'h'
Returns
TypeDescription
list[str]A list of fully-qualified parquet paths.
Raises
TypeDescription
LoaderErrorIf no UUIDs are supplied.
ValueErrorFor an empty or blank uuid.

load_by_time_range_and_uuids​

methodsource
load_by_time_range_and_uuids(
start: str | pd.Timestamp,
end: str | pd.Timestamp,
uuids: str | Sequence[str],
*,
columns: Sequence[str] | None = None,
filter_expr: str | None = None,
freq: str = "h",
ignore_missing: bool = True,
path_exists: Callable[[str], bool] | None = None,
as_pandas: bool = True
) -> Any

Read the hourly parquet files for uuids within [start, end].

Parameters
NameTypeDescriptionDefault
startstr | TimestampWindow start.required
endstr | TimestampWindow end (inclusive).required
uuidsstr | Sequence[str]A single UUID or a sequence of UUIDs.required
columnsSequence[str] | NoneOptional columns to select (projection pushdown).None
filter_exprstr | NoneOptional Spark SQL predicate applied with .where, e.g. "value_integer > 0".None
freqstrFolder granularity; defaults to hourly.'h'
ignore_missingboolWhen True, set spark.sql.files.ignoreMissingFiles so hours with no file for a UUID are skipped instead of failing.True
path_existsCallable[[str], bool] | NoneOptional callable used to pre-filter paths to those that exist (e.g. a wrapper over dbutils.fs). Useful when ignore_missing is not sufficient for your storage layer.None
as_pandasboolWhen True (default) return a pandas DataFrame via toPandas(); when False return the Spark DataFrame.True
Returns
TypeDescription
AnyA pandas DataFrame, or the Spark DataFrame when as_pandas is
AnyFalse. An empty pandas DataFrame is returned if no candidate paths
Anyremain after path_exists filtering.
Raises
TypeDescription
LoaderErrorIf no SparkSession is available.

fetch_data_as_dataframe​

methodsource
fetch_data_as_dataframe(
start: str | pd.Timestamp,
end: str | pd.Timestamp,
uuids: str | Sequence[str],
*,
columns: Sequence[str] | None = None,
filter_expr: str | None = None
) -> pd.DataFrame

Pandas-returning convenience for Pipeline / DataIntegratorHybrid.

Parameters
NameTypeDescriptionDefault
startstr | TimestampWindow start.required
endstr | TimestampWindow end (inclusive).required
uuidsstr | Sequence[str]A single UUID or a sequence of UUIDs.required
columnsSequence[str] | NoneOptional columns to select (projection pushdown).None
filter_exprstr | NoneOptional Spark SQL predicate applied with .where, e.g. "value_integer > 0".None
Returns
TypeDescription
DataFramePandas DataFrame from load_by_time_range_and_uuids with as_pandas=True.