mirror of
https://github.com/apache/superset.git
synced 2026-04-09 19:35:21 +00:00
55 lines
2.3 KiB
Python
55 lines
2.3 KiB
Python
# Licensed to the Apache Software Foundation (ASF) under one
|
|
# or more contributor license agreements. See the NOTICE file
|
|
# distributed with this work for additional information
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
# to you under the Apache License, Version 2.0 (the
|
|
# "License"); you may not use this file except in compliance
|
|
# with the License. You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing,
|
|
# software distributed under the License is distributed on an
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
# KIND, either express or implied. See the License for the
|
|
# specific language governing permissions and limitations
|
|
# under the License.
|
|
from typing import Dict, List, Optional
|
|
|
|
from pandas import DataFrame
|
|
|
|
from superset.utils.pandas_postprocessing.utils import validate_column_args
|
|
|
|
|
|
@validate_column_args("columns", "drop", "rename")
|
|
def select(
|
|
df: DataFrame,
|
|
columns: Optional[List[str]] = None,
|
|
exclude: Optional[List[str]] = None,
|
|
rename: Optional[Dict[str, str]] = None,
|
|
) -> DataFrame:
|
|
"""
|
|
Only select a subset of columns in the original dataset. Can be useful for
|
|
removing unnecessary intermediate results, renaming and reordering columns.
|
|
|
|
:param df: DataFrame on which the rolling period will be based.
|
|
:param columns: Columns which to select from the DataFrame, in the desired order.
|
|
If left undefined, all columns will be selected. If columns are
|
|
renamed, the original column name should be referenced here.
|
|
:param exclude: columns to exclude from selection. If columns are renamed, the new
|
|
column name should be referenced here.
|
|
:param rename: columns which to rename, mapping source column to target column.
|
|
For instance, `{'y': 'y2'}` will rename the column `y` to
|
|
`y2`.
|
|
:return: Subset of columns in original DataFrame
|
|
:raises QueryObjectValidationError: If the request in incorrect
|
|
"""
|
|
df_select = df.copy(deep=False)
|
|
if columns:
|
|
df_select = df_select[columns]
|
|
if exclude:
|
|
df_select = df_select.drop(exclude, axis=1)
|
|
if rename is not None:
|
|
df_select = df_select.rename(columns=rename)
|
|
return df_select
|