← Back to playground index

update

Update a Series or DataFrame in-place using non-NA values from another object. Mirrors pandas.DataFrame.update and pandas.Series.update.

seriesUpdate — basic overwrite

Python pandas equivalent:

import pandas as pd
import numpy as np

s = pd.Series([1, np.nan, 3], index=[0, 1, 2])
other = pd.Series([np.nan, 20, np.nan], index=[0, 1, 2])
s.update(other)
print(s.tolist())
# [1.0, 20.0, 3.0]

tsb equivalent:

import { Series, seriesUpdate } from "tsb";

const s = new Series({ data: [1, null, 3], index: [0, 1, 2] });
const other = new Series({ data: [null, 20, null], index: [0, 1, 2] });
seriesUpdate(s, other).values;
// [1, 20, 3]

overwrite=false — only fill NA

Python pandas equivalent:

import pandas as pd
import numpy as np

s = pd.Series([1, np.nan, 3])
other = pd.Series([10, 20, 30])
s.update(other, overwrite=False)
print(s.tolist())
# [1.0, 20.0, 3.0]

tsb equivalent:

import { Series, seriesUpdate } from "tsb";

const s = new Series({ data: [1, null, 3] });
const other = new Series({ data: [10, 20, 30] });
seriesUpdate(s, other, { overwrite: false }).values;
// [1, 20, 3]

dataFrameUpdate — update from another DataFrame

Python pandas equivalent:

import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": [10, 20, 30]})
other = pd.DataFrame({"a": [np.nan, 99, np.nan]})
df.update(other)
print(df)
#      a     b
# 0  1.0  10.0
# 1  99.0  20.0
# 2  3.0  30.0

tsb equivalent:

import { DataFrame, dataFrameUpdate } from "tsb";

const df = DataFrame.fromColumns({ a: [1, null, 3], b: [10, 20, 30] });
const other = DataFrame.fromColumns({ a: [null, 99, null] });
const result = dataFrameUpdate(df, other);
result.col("a").values; // [1, 99, 3]
result.col("b").values; // [10, 20, 30]

Label alignment

tsb equivalent:

import { Series, seriesUpdate } from "tsb";

const s = new Series({ data: [1, 2, 3], index: [0, 1, 2] });
// other only has label 1 — other labels unchanged
const other = new Series({ data: [99], index: [1] });
seriesUpdate(s, other).values;
// [1, 99, 3]