-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #720 from lsst/tickets/DM-35803
DM-35803: Add DataFrame delegate to enable InMemoryDatasets of DataFrames.
- Loading branch information
Showing
7 changed files
with
259 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a bug in the parquet reader where a single string column name would be interpreted as an iterable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added ``DataFrameDelegate`` to allow DataFrames to be used with ``lsst.pipe.base.InMemoryDatasetHandle``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Support for reading DataFrames.""" | ||
from __future__ import annotations | ||
|
||
import collections.abc | ||
from typing import Any, Mapping, Optional | ||
|
||
import pandas | ||
from lsst.daf.butler import StorageClassDelegate | ||
from lsst.utils.introspection import get_full_type_name | ||
from lsst.utils.iteration import ensure_iterable | ||
|
||
__all__ = ["DataFrameDelegate"] | ||
|
||
|
||
class DataFrameDelegate(StorageClassDelegate): | ||
def getComponent(self, composite: pandas.DataFrame, componentName: str) -> Any: | ||
"""Get a component from a DataFrame. | ||
Parameters | ||
---------- | ||
composite : `~pandas.DataFrame` | ||
``DataFrame`` to access component. | ||
componentName : `str` | ||
Name of component to retrieve. | ||
Returns | ||
------- | ||
component : `object` | ||
The component. | ||
Raises | ||
------ | ||
AttributeError | ||
The component can not be found. | ||
""" | ||
if componentName == "columns": | ||
return pandas.Index(self._getAllColumns(composite)) | ||
else: | ||
raise AttributeError( | ||
f"Do not know how to retrieve component {componentName} from {get_full_type_name(composite)}" | ||
) | ||
|
||
def handleParameters( | ||
self, inMemoryDataset: pandas.DataFrame, parameters: Optional[Mapping[str, Any]] = None | ||
) -> Any: | ||
"""Return possibly new in-memory dataset using the supplied parameters. | ||
Parameters | ||
---------- | ||
inMemoryDataset : `object` | ||
Object to modify based on the parameters. | ||
parameters : `dict`, optional | ||
Parameters to apply. Values are specific to the parameter. | ||
Supported parameters are defined in the associated | ||
`StorageClass`. If no relevant parameters are specified the | ||
``inMemoryDataset`` will be return unchanged. | ||
Returns | ||
------- | ||
inMemoryDataset : `object` | ||
Original in-memory dataset, or updated form after parameters | ||
have been used. | ||
""" | ||
if not isinstance(inMemoryDataset, pandas.DataFrame): | ||
raise ValueError( | ||
"handleParameters for a DataFrame must get a DataFrame, " | ||
f"not {get_full_type_name(inMemoryDataset)}." | ||
) | ||
|
||
if parameters is None: | ||
return inMemoryDataset | ||
|
||
if "columns" in parameters: | ||
allColumns = self._getAllColumns(inMemoryDataset) | ||
|
||
if not isinstance(parameters["columns"], collections.abc.Iterable): | ||
raise NotImplementedError( | ||
"InMemoryDataset of a DataFrame only supports list/tuple of string column names" | ||
) | ||
|
||
for column in ensure_iterable(parameters["columns"]): | ||
if not isinstance(column, str): | ||
raise NotImplementedError( | ||
"InMemoryDataset of a DataFrame only supports string column names." | ||
) | ||
if column not in allColumns: | ||
raise ValueError(f"Unrecognized column name {column!r}.") | ||
|
||
# Exclude index columns from the subset. | ||
readColumns = [ | ||
name | ||
for name in ensure_iterable(parameters["columns"]) | ||
if name not in inMemoryDataset.index.names | ||
] | ||
|
||
return inMemoryDataset[readColumns] | ||
else: | ||
return inMemoryDataset | ||
|
||
def _getAllColumns(self, inMemoryDataset: pandas.DataFrame) -> list[str]: | ||
"""Get all columns, including index columns. | ||
Returns | ||
------- | ||
columns : `list` [`str`] | ||
List of all columns. | ||
""" | ||
allColumns = list(inMemoryDataset.columns) | ||
if inMemoryDataset.index.names[0] is not None: | ||
allColumns.extend(inMemoryDataset.index.names) | ||
|
||
return allColumns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters