2

some_series.update(other_series) will overwrite all values on some_series with their matching (by index) values from other_series. However, it does skip all NA values like None.

What should I use if I want to update some_series in place from other_series, but also have the NA values apply?

To clarify, this is what I want:

In [197]: some_series
Out[197]: 
0    1
1    2
dtype: int64

In [198]: other_series
Out[198]: 
0    None
dtype: object

In [203]: some_series  # after overriding it with other_series:
Out[203]: 
0    None
1       2
dtype: object
Dun Peal
  • 16,679
  • 11
  • 33
  • 46
  • Isn't that what `some_series = other_series` will do? – EdChum May 06 '15 at 21:42
  • Are you using update because the span of the index is different for the two series? If not, I would just use some_series = other_series.copy() – Mark Graph May 06 '15 at 21:45
  • @MarkGraph I don't think the explicit call to `copy()` is necessary here, I tested it and it does create a reference to the other series – EdChum May 06 '15 at 21:47
  • @EdChum: doesn't seem so: `some_series = Series([1, 2]); other_series = Series([None]); some_series = other_series #=> (0 None)`. What I need is `(0 None, 1 2)`. – Dun Peal May 06 '15 at 21:47
  • I don't understand how you'd expect that kind of result as it looks like you want something like `pd.concat([other_series, some_series], ignore_index=True)` rather than trying to overwrite the lhs – EdChum May 06 '15 at 21:51
  • Where did the zero in your desired concatenation come from? – Mark Graph May 06 '15 at 21:52
  • @EdChum: sorry for the confusion, check the edited question description for an exact definition of the behavior I expect. – Dun Peal May 06 '15 at 21:56
  • @MarkGraph: sorry for being unclear, see updated description. – Dun Peal May 06 '15 at 21:56
  • One way that would work would be `some_series.loc[other_series.index] = other_series` – EdChum May 06 '15 at 22:02

3 Answers3

1

I'm not sure this is the nicest way, but here's one way. You have to ensure that the Series you want to update includes that of the other (e.g. with reindex).

In [11]: s
Out[11]:
0    1
1    2
dtype: int64

In [12]: t
Out[12]:
1   NaN
2     3
3     4
dtype: float64

In [13]: res = s.reindex(s.index.union(t.index))

Note: without this stage, if you just try to update s, you'll get a KeyError.

In [14]: res.loc[t.index] = t

In [25]: res
Out[25]:
0     1
1   NaN
2     3
3     4
dtype: float64

To update just the index of s use the intersection:

In [21]: ind = t.index.intersection(s.index)

In [22]: s.loc[ind] = t.loc[ind]

In [23]: s
Out[23]:
0     1
1   NaN
dtype: float64
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Thanks, but I actually don't want any labels on `t` that aren't in `s` to be in `res`. In other words, all I want is to override values in `s` with their corresponding (by label) `t` values. – Dun Peal May 06 '15 at 22:01
  • 1
    @DunPeal updated (use intersection rather than union) – Andy Hayden May 06 '15 at 22:14
1

the following would work, this uses loc and the other series index to mask the elements we want to overwrite:

In [106]:

some_series = pd.Series([1, 2])
other_series = pd.Series([None])
some_series.loc[other_series.index] = other_series
some_series
Out[106]:
0   NaN
1     2
dtype: float64

The loc is acutally unneccessary in this case

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Thanks! What do you mean by "The loc is acutally unneccessary in this case"? – Dun Peal May 06 '15 at 22:07
  • I mean that `some_series[other_series.index] = other_series` would also work – EdChum May 06 '15 at 22:09
  • If `other_series` includes indices outside of `some_series` this is a KeyError! :( – Andy Hayden May 06 '15 at 22:31
  • @AndyHayden true but this is so ill-defined that I've given up polishing this any further, plus I'm watching 30rock and deciding how to waste my vote tomorrow – EdChum May 06 '15 at 22:33
  • @EdChum ha, you call this one ill-defined - (I [raise](http://stackoverflow.com/q/29938321/1240268))! ...not looking forward to waking up to that result. Fingers crossed aye. – Andy Hayden May 06 '15 at 23:54
0
some_series[other_series.index] = other_series
Mark Graph
  • 4,969
  • 6
  • 25
  • 37