PartitionedDataset¶
PartitionedDataset is used to manage datasets that are partitioned into multiple files or directories.
kedro_datasets.partitions.PartitionedDataset ¶
PartitionedDataset(
*,
path,
dataset,
filepath_arg="filepath",
filename_suffix="",
credentials=None,
load_args=None,
fs_args=None,
overwrite=False,
save_lazily=True,
metadata=None
)
Bases: AbstractDataset[dict[str, Any], dict[str, Callable[[], Any]]]
PartitionedDataset loads and saves partitioned file-like data using the
underlying dataset definition. For filesystem level operations it uses fsspec:
https://github.com/intake/filesystem_spec.
It also supports advanced features like lazy saving.
Examples:
Using the YAML API:
station_data:
type: partitions.PartitionedDataset
path: data/03_primary/station_data
dataset:
type: pandas.CSVDataset
load_args:
sep: '\t'
save_args:
sep: '\t'
index: true
filename_suffix: '.dat'
save_lazily: True
Using the Python API:
>>> import pandas as pd
>>> from kedro_datasets.partitions import PartitionedDataset
>>>
>>> # Create a fake pandas dataframe with 10 rows of data
>>> df = pd.DataFrame([{"DAY_OF_MONTH": str(i), "VALUE": i} for i in range(1, 11)])
>>>
>>> # Convert it to a dict of pd.DataFrame with DAY_OF_MONTH as the dict key
>>> dict_df = {
... day_of_month: df[df["DAY_OF_MONTH"] == day_of_month]
... for day_of_month in df["DAY_OF_MONTH"]
... }
>>>
>>> # Save it as small partitions with DAY_OF_MONTH as the partition key
>>> dataset = PartitionedDataset(
... path=str(tmp_path / "df_with_partition"),
... dataset="pandas.CSVDataset",
... filename_suffix=".csv",
... save_lazily=False,
... )
>>> # This will create a folder `df_with_partition` and save multiple files
>>> # with the dict key + filename_suffix as filename, i.e. 1.csv, 2.csv etc.
>>> dataset.save(dict_df)
>>>
>>> # This will create lazy load functions instead of loading data into memory immediately.
>>> loaded = dataset.load()
>>>
>>> # Load all the partitions
>>> for partition_id, partition_load_func in loaded.items():
... # The actual function that loads the data
... partition_data = partition_load_func()
...
>>> # Add the processing logic for individual partition HERE
>>> # print(partition_data)
You can also load multiple partitions from a remote storage and combine them like this:
>>> import pandas as pd
>>> from kedro_datasets.partitions import PartitionedDataset
>>>
>>> # these credentials will be passed to both 'fsspec.filesystem()' call
>>> # and the dataset initializer
>>> credentials = {"key1": "secret1", "key2": "secret2"}
>>>
>>> dataset = PartitionedDataset(
... path="s3://bucket-name/path/to/folder",
... dataset="pandas.CSVDataset",
... credentials=credentials,
... )
>>> loaded = dataset.load()
>>> # assert isinstance(loaded, dict)
>>>
>>> combine_all = pd.DataFrame()
>>>
>>> for partition_id, partition_load_func in loaded.items():
... partition_data = partition_load_func()
... combine_all = pd.concat([combine_all, partition_data], ignore_index=True, sort=True)
...
>>> new_data = pd.DataFrame({"new": [1, 2]})
>>> # creates "s3://bucket-name/path/to/folder/new/partition.csv"
>>> dataset.save({"new/partition.csv": new_data})
Parameters:
-
path(str) –Path to the folder containing partitioned data. If path starts with the protocol (e.g.,
s3://) then the correspondingfsspecconcrete filesystem implementation will be used. If protocol is not specified,fsspec.implementations.local.LocalFileSystemwill be used. Note: Some concrete implementations are bundled withfsspec, while others (likes3orgcs) must be installed separately prior to usage of thePartitionedDataset. -
dataset(str | type[AbstractDataset] | dict[str, Any]) –Underlying dataset definition. This is used to instantiate the dataset for each file located inside the
path. Accepted formats are: a) object of a class that inherits fromAbstractDatasetb) a string representing a fully qualified class name to such class c) a dictionary withtypekey pointing to a string from b), other keys are passed to the Dataset initializer. Credentials for the dataset can be explicitly specified in this configuration. -
filepath_arg(str, default:'filepath') –Underlying dataset initializer argument that will contain a path to each corresponding partition file. If unspecified, defaults to "filepath".
-
filename_suffix(str, default:'') –If specified, only partitions that end with this string will be processed.
-
credentials(dict[str, Any] | None, default:None) –Protocol-specific options that will be passed to
fsspec.filesystemhttps://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.filesystem and the dataset initializer. If the dataset config contains explicit credentials spec, then such spec will take precedence. All possible credentials management scenarios are documented here: https://docs.kedro.org/en/stable/data/partitioned_and_incremental_datasets.html#partitioned-dataset-credentials -
load_args(dict[str, Any] | None, default:None) –Keyword arguments to be passed into
find()method of the filesystem implementation. -
fs_args(dict[str, Any] | None, default:None) –Extra arguments to pass into underlying filesystem class constructor (e.g.
{"project": "my-project"}forGCSFileSystem). -
overwrite(bool, default:False) –If True, any existing partitions will be removed.
-
save_lazily(bool, default:True) –Parameter to enable/disable lazy saving, the default is True. Meaning that if callable object is passed as data to save, the partition’s data will not be materialised until it is time to write. Lazy saving example: https://docs.kedro.org/en/stable/catalog-data/partitioned_and_incremental_datasets/#partitioned-dataset-lazy-saving
-
metadata(dict[str, Any] | None, default:None) –Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.
Raises:
-
DatasetError–If versioning is enabled for the underlying dataset.
Source code in kedro_datasets/partitions/partitioned_dataset.py
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 | |
__repr__ ¶
__repr__()
Source code in kedro_datasets/partitions/partitioned_dataset.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
_describe ¶
_describe()
Source code in kedro_datasets/partitions/partitioned_dataset.py
338 339 340 341 342 343 344 345 346 347 348 | |
_exists ¶
_exists()
Source code in kedro_datasets/partitions/partitioned_dataset.py
370 371 | |
_invalidate_caches ¶
_invalidate_caches()
Source code in kedro_datasets/partitions/partitioned_dataset.py
366 367 368 | |
_join_protocol ¶
_join_protocol(path)
Source code in kedro_datasets/partitions/partitioned_dataset.py
270 271 272 273 274 275 276 | |
_list_partitions ¶
_list_partitions()
Source code in kedro_datasets/partitions/partitioned_dataset.py
258 259 260 261 262 263 264 265 266 267 268 | |
_partition_to_path ¶
_partition_to_path(path)
Source code in kedro_datasets/partitions/partitioned_dataset.py
278 279 280 281 282 283 284 285 286 287 288 | |
_path_to_partition ¶
_path_to_partition(path)
Source code in kedro_datasets/partitions/partitioned_dataset.py
290 291 292 293 294 295 296 297 298 299 300 301 | |
_release ¶
_release()
Source code in kedro_datasets/partitions/partitioned_dataset.py
373 374 375 | |
load ¶
load()
Source code in kedro_datasets/partitions/partitioned_dataset.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
save ¶
save(data)
Source code in kedro_datasets/partitions/partitioned_dataset.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |