2

This turned out to be non-trivial for me so I wanted to check if others have a simple solution for this:

Suppose I have an arbitrary number (say 3) of pd.Series: which look like:

first = pd.Series(range(5))
second = pd.Series(range(7))
third = pd.Series(range(6))

I'd like to make them all of the same length (7 -- which is the largest length) and pad the shorter ones with np.nans either at the top (optionally at the bottom) so that first looks like:

nan
nan
  0
  1
  2
  3
  4

and so on.

asb
  • 4,392
  • 1
  • 20
  • 30

1 Answers1

4

You could use reindex to give each Series a new index. If the new index contains labels which are not in the original series' index, then a NaN value is filled in (unless a different fill_value is specified):

In [15]: first.reindex(range(7))
Out[15]: 
0    0.0
1    1.0
2    2.0
3    3.0
4    4.0
5    NaN
6    NaN
dtype: float64

You can control the placement of the NaNs by your choice of reindexing labels:

In [19]: first.reindex(range(-2,5))
Out[19]: 
-2    NaN
-1    NaN
 0    0.0
 1    1.0
 2    2.0
 3    3.0
 4    4.0
dtype: float64

Note that the inclusion of NaNs forces the dtype of first to be promoted from an integer dtype to a floating-point dtype since NaNs are floats (and hence Series of integer dtype can not contain NaNs).

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thanks. I had a hunch but was not able to fix it. I need to wait for a while before accepting the answer. – asb Aug 03 '16 at 12:11