Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make interpolate pad method use ffill
  • Loading branch information
TrevorBergeron committed Nov 2, 2023
commit 0e30360cd00c142efa19247f6084b079b7a1229e
11 changes: 6 additions & 5 deletions bigframes/core/block_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bigframes.core.blocks as blocks
import bigframes.core.ordering as ordering
import bigframes.core.window_spec as windows
import bigframes.dtypes as dtypes
import bigframes.operations as ops
import bigframes.operations.aggregations as agg_ops

Expand Down Expand Up @@ -113,7 +114,6 @@ def interpolate(block: blocks.Block, method: str = "linear") -> blocks.Block:
"nearest",
"zero",
"slinear",
"pad",
]
if method not in supported_methods:
raise NotImplementedError(
Expand All @@ -131,7 +131,9 @@ def interpolate(block: blocks.Block, method: str = "linear") -> blocks.Block:
if len(index_columns) != 1:
raise ValueError("only method 'linear' support multi-index")
xvalues = block.index_columns[0]
# TODO: verify numeric
dtypes.NUMERIC_BIGFRAMES_TYPES
if block.index_dtypes[0] not in dtypes.NUMERIC_BIGFRAMES_TYPES:
raise ValueError("Can only interpolate on numeric index.")

for column in original_columns:
# null in same places column is null
Expand All @@ -145,21 +147,20 @@ def interpolate(block: blocks.Block, method: str = "linear") -> blocks.Block:
"values": "linear",
"index": "linear",
"slinear": "linear",
"pad": "ffill",
"zero": "ffill",
"nearest": "nearest",
}
extrapolating_methods = ["linear", "values", "index"]
interpolate_method = interpolate_method_map[method]
do_extrapolate = method in extrapolating_methods
block, interpolated_and_ffilled = _interpolate_column(
block, interpolated = _interpolate_column(
block,
column,
xvalues,
interpolate_method=interpolate_method,
do_extrapolate=do_extrapolate,
)
output_column_ids.append(interpolated_and_ffilled)
output_column_ids.append(interpolated)
else:
output_column_ids.append(column)

Expand Down
2 changes: 2 additions & 0 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,8 @@ def reindex_like(self, other: DataFrame, *, validate: typing.Optional[bool] = No
return self.reindex(index=other.index, columns=other.columns, validate=validate)

def interpolate(self, method: str = "linear") -> DataFrame:
if method == "pad":
return self.ffill()
result = block_ops.interpolate(self._block, method)
return DataFrame(result)

Expand Down
2 changes: 2 additions & 0 deletions bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ def replace(
return Series(block.select_column(result_col))

def interpolate(self, method: str = "linear") -> Series:
if method == "pad":
return self.ffill()
result = block_ops.interpolate(self._block, method)
return Series(result)

Expand Down
5 changes: 5 additions & 0 deletions tests/system/small/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ def test_series_interpolate(method):
pd_result = pd_series.astype("float64").interpolate(method=method)
bf_result = bf_series.interpolate(method=method).to_pandas()

print("pandas")
print(pd_result.to_string())
print("bigframes")
print(bf_result.to_string())

# pd uses non-null types, while bf uses nullable types
pd.testing.assert_series_equal(
pd_result,
Expand Down