Skip to content

Commit d4b4497

Browse files
committed
Add methods for filling missing values
1 parent d5c79fb commit d4b4497

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

core/src/main/java/tech/tablesaw/api/DoubleColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public String getUnformattedString(final int row) {
300300
}
301301

302302
@Override
303-
public NumberColumn emptyCopy() {
303+
public DoubleColumn emptyCopy() {
304304
return emptyCopy(DEFAULT_ARRAY_SIZE);
305305
}
306306

core/src/main/java/tech/tablesaw/columns/AbstractColumn.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ public ColumnType type() {
6262
return type;
6363
}
6464

65+
@Override
66+
public abstract C emptyCopy();
67+
68+
/**
69+
* Create a copy of this column where missing values are replaced with the given default value
70+
*/
71+
public C fillMissing(T defaultVal) {
72+
C newCol = emptyCopy();
73+
for (int i = 0; i < this.size(); i++) {
74+
if (isMissing(i)) {
75+
newCol.set(i, defaultVal);
76+
} else {
77+
newCol.set(1, getObject(i));
78+
}
79+
}
80+
return newCol;
81+
}
82+
83+
/**
84+
* Create a copy of this column where missing values are replaced with the corresponding value in the given column
85+
*/
86+
public C fillMissing(C other) {
87+
C newCol = emptyCopy();
88+
for (int i = 0; i < this.size(); i++) {
89+
if (isMissing(i)) {
90+
newCol.set(i, other.getObject(i));
91+
} else {
92+
newCol.set(1, getObject(i));
93+
}
94+
}
95+
return newCol;
96+
}
97+
6598
public abstract C set(int i, T val);
6699

67100
public abstract T getObject(int i);

0 commit comments

Comments
 (0)