Skip to content

Commit

Permalink
feat(df): adding fillna with value method
Browse files Browse the repository at this point in the history
  • Loading branch information
mariogarcia committed Dec 17, 2024
1 parent 3620847 commit 3a8df82
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ import java.util.function.Function
* @since 0.1.0
*/
interface DataFrame extends Columnar {

/**
* Make a copy of this object’s indices and data.
*
* @return a new instance of {@link DataFrame}
* @since 0.1.0
*/
DataFrame copy()

/**
* Fill NA/NaN values using the specified value passed as parameter
*
* @param value the value to replace the NA/Nan values with
* @return the dataframe with replaced values
* @since 0.1.0
*/
DataFrame fillna(Object value)

/**
* The transpose of the DataFrame.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ interface Series extends Columnar, Iterable {
*/
Series dropna()

/**
* Fill NA/NaN values using the specified value
*
* @param value the value to replace the NA/Nan values with
* @return the series instance with replaced values
* @since 0.1.0
*/
Series fillna(Object value)

/**
* @param index
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ class TSDataFrame implements DataFrame {
return new TSDataFrame(Table.create(dataFrameName, columns))
}

@Override
DataFrame copy() {
return new TSDataFrame(table.copy())
}

@Override
DataFrame fillna(Object o) {
Table copied = table.copy()
copied.columns().each {
if (it.type() == ColumnType.DOUBLE && o.toString().isNumber() && o.toString().isLong()) {
it.setMissingTo(o.toString().toDouble())
} else if (it.type() == ColumnType.STRING) {
it.setMissingTo(o.toString())
} else {
it.setMissingTo(o)
}
}
return new TSDataFrame(copied)
}

@Override
DataFrame getT() {
return new TSDataFrame(this.table.transpose())
Expand Down Expand Up @@ -487,7 +507,7 @@ class TSDataFrame implements DataFrame {
@NamedVariant
Series min(TypeAxis axisType) {
return null
min }
}

@Override
DataFrame minus(Series series) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ class TSSeries implements Series {
return new TSSeries(column.removeMissing())
}

@Override
Series fillna(Object o) {
return new TSSeries(column.setMissingTo(o))
}

@Override
Series lag(int index) {
return new TSSeries(this.column.lag(index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,38 @@ class DataFrameSpec extends BaseSpec {
columns == ['id', 'weight']
weights == [11, 22, 303, 604, 1005] as int[]
}

def "[DataFrame/fillna]: fixed value all numeric series"() {
setup:
def df = [
A: [1, 2, null, 3, 4],
B: [1.0, 2.0, 3.0, null, 4.0],
C: ((1..4).collect { new BigDecimal(it) } + [null])
].toDataFrame("coercing")

when:
df = df.fillna(-1)

then:
df['A'].toList() == [1, 2, -1, 3, 4]
df['B'].toList() == [1.0, 2.0, 3.0, -1, 4.0]
df['C'].toList() == [1.0, 2.0, 3.0, 4.0, -1.0]
}

def "[DataFrame/fillna]: fixed value all mixed series"() {
setup:
def df = [
A: ('a'..'c') + [null],
B: [1.0, 2.0, null, 4.0],
C: (1..3) + [null]
].toDataFrame("coercing")

when:
df = df.fillna(-1)

then:
df['A'].toList() == ['a', 'b', 'c', '-1']
df['B'].toList() == [1.0, 2.0, -1, 4.0]
df['C'].toList() == [1, 2, 3, -1.0]
}
}

0 comments on commit 3a8df82

Please sign in to comment.