Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rolling window result centering #889

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions core/src/main/java/tech/tablesaw/columns/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ default RollingColumn rolling(final int windowSize) {
return new RollingColumn(this, windowSize);
}

/**
* @param windowSize size of the rolling window to use
* @param center set the operation result at the center of the window or at the end of it
*/
default RollingColumn rolling(final int windowSize, final boolean center) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could use a javadoc for people not familiar with pandas's center

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies if this is duplicating something i wrote elsewhere. I made a comment but don't see it now, so perhaps i didn't save it.

I think the javadoc for this method needs to incorporate a slightly shorter version of the explanation you wrote for the PR. The comment here isn't enough for anyone to really get what it does, but the explanation you wrote is very clear. If you can add that, I think we can resolve this and approve and merge the PR

return new RollingColumn(this, windowSize, center);
}

String getUnformattedString(int r);

boolean isMissing(int rowNumber);
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/java/tech/tablesaw/table/RollingColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ public class RollingColumn {

protected final Column<?> column;
protected final int window;
protected final boolean center;

public RollingColumn(Column<?> column, int window) {
public RollingColumn(Column<?> column, int window, boolean center) {
this.column = column;
this.window = window;
this.center = center;
}

public RollingColumn(Column<?> column, int window) {
this(column, window, false);
}

protected String generateNewColumnName(AggregateFunction<?, ?> function) {
Expand All @@ -31,7 +37,9 @@ protected String generateNewColumnName(AggregateFunction<?, ?> function) {
public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) {
// TODO: the subset operation copies the array. creating a view would likely be more efficient
Column<?> result = function.returnType().create(generateNewColumnName(function));
for (int i = 0; i < window - 1; i++) {
int bound = center ? (window - 1) / 2 : window - 1;

for (int i = 0; i < bound; i++) {
result.appendMissing();
}
for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) {
Expand All @@ -46,6 +54,11 @@ public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OU
result.appendObj(answer);
}
}
if (center) {
for (int i = 0; i < bound; i++) {
result.appendMissing();
}
}
return result;
}
}
20 changes: 16 additions & 4 deletions core/src/test/java/tech/tablesaw/table/RollingColumnTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tech.tablesaw.table;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static tech.tablesaw.aggregate.AggregateFunctions.countTrue;
import static tech.tablesaw.aggregate.AggregateFunctions.latestDateTime;
import static org.junit.jupiter.api.Assertions.*;
import static tech.tablesaw.aggregate.AggregateFunctions.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -52,4 +50,18 @@ public void testRollingCountTrue() {
assertEquals(1, result.getDouble(3), 0.0);
assertEquals(2, result.getDouble(4), 0.0);
}

@Test
public void testCenter() {
double[] data = new double[] {1, 2, 3, 4, 5};
DoubleColumn doubleColumn = DoubleColumn.create("data", data);

DoubleColumn result = (DoubleColumn) doubleColumn.rolling(3, true).calc(mean);

assertEquals(Double.NaN, result.getDouble(0), 0.0);
assertNotEquals(Double.NaN, result.getDouble(1), 0.0);
assertNotEquals(Double.NaN, result.getDouble(2), 0.0);
assertNotEquals(Double.NaN, result.getDouble(3), 0.0);
assertEquals(Double.NaN, result.getDouble(4), 0.0);
}
}