[BUG] Pandas mean/sum/stdev fail on a Decimal column when the DataFrame index isn't a default RangeIndex
Describe the bug
convert_pandas_series_decimal_to_float_dtype (great_expectations/util.py:852) converts Decimal values to float before column.mean, column.sum and column.standard_deviation run on pandas. It writes the result back with data.update(pd.Series(series_data)). The new Series has a fresh 0..n-1 index and update aligns by label, so if the column's index is anything else the converted values land on the wrong rows, or on none.
This happens with a custom DataFrame index, or when a row_condition filters rows and leaves gaps in the index. The validation then fails with TypeError: unsupported operand type(s) ... 'float' and 'decimal.Decimal'.
Expected: same result as with a default RangeIndex, which works fine today.
Great Expectations version
1.23.1
Python version
3.13.3
Execution engine / data source
Pandas
If "Other", name the execution engine or data source
No response
Minimal reproduction
- Create a pandas DataFrame asset with a Decimal column.
- Either give the DataFrame a non-default index, or validate with a
row_conditionthat filters rows. - Validate
ExpectColumnStdevToBeBetween/ExpectColumnSumToBeBetween. The metric fails withMetricResolutionError.
from decimal import Decimal
import pandas as pd
import great_expectations as gx
import great_expectations.expectations as gxe
from great_expectations.util import convert_pandas_series_decimal_to_float_dtype
s = pd.Series([Decimal("5"), Decimal("1"), Decimal("2")], index=[0, 2, 3])
convert_pandas_series_decimal_to_float_dtype(data=s, inplace=True)
print(s.tolist())
context = gx.get_context(mode="ephemeral")
bd = context.data_sources.add_pandas("pd").add_dataframe_asset("df").add_batch_definition_whole_dataframe("all")
# Decimal column with its own index
df1 = pd.DataFrame({"amount": [Decimal("1"), Decimal("2"), Decimal("100")]}, index=[10, 11, 12])
r1 = bd.get_batch(batch_parameters={"dataframe": df1}).validate(
gxe.ExpectColumnStdevToBeBetween(column="amount", min_value=0, max_value=1000)
)
# Default index, filtered by row_condition
df2 = pd.DataFrame({"region": ["eu", "us", "eu", "eu"],
"amount": [Decimal("5"), Decimal("7"), Decimal("1"), Decimal("2")]})
r2 = bd.get_batch(batch_parameters={"dataframe": df2}).validate(
gxe.ExpectColumnSumToBeBetween(column="amount", min_value=0, max_value=100,
row_condition='region == "eu"', condition_parser="pandas")
)
for r in (r1, r2):
print(r.success, [v["exception_message"] for v in r.exception_info.values() if isinstance(v, dict)])# sample data (inline above)
df1: amount=[1, 2, 100] as Decimal, index=[10, 11, 12]
df2: region=["eu", "us", "eu", "eu"], amount=[5, 7, 1, 2] as Decimal# output
[5.0, 2.0, Decimal('2')] <- the 1 was overwritten by the 2
False ["unsupported operand type(s) for -: 'float' and 'decimal.Decimal'"]
False ["unsupported operand type(s) for +: 'float' and 'decimal.Decimal'"]
# stack trace (stdev case, trimmed)
File ".../great_expectations/expectations/metrics/column_aggregate_metrics/column_standard_deviation.py", line 44, in _pandas
return column.std()
File ".../pandas/core/nanops.py", line 1028, in nanvar
sqr = _ensure_numeric((avg - values) ** 2)
TypeError: unsupported operand type(s) for -: 'float' and 'decimal.Decimal'
The above exception was the direct cause of the following exception:
File ".../great_expectations/execution_engine/execution_engine.py", line 582, in _process_direct_and_bundled_metric_computation_configurations
raise gx_exceptions.MetricResolutionError(
great_expectations.exceptions.exceptions.MetricResolutionError: unsupported operand type(s) for -: 'float' and 'decimal.Decimal'Additional environment details
Windows 11, pandas 3.0.6, numpy 2.5.3. Also reproduces on develop (ca1c660c6).
Additional context
Passing the original index through, pd.Series(series_data, index=data.index), fixes it locally. Not related to #12251, which is about Infinity values on SQL.
Source: fivetran/great_expectations