user1915817
user1915817

Reputation: 419

How to index a DataFrame with "holes" with a TimeSeries without holes

I have a DataFrame indexed by dates like this:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1853141 entries, 2012-03-01 00:00:00 to 2012-06-16 23:59:55
Data columns:
Open Bid ESM2    1853141  non-null values
Open Ask ESM2    1853141  non-null values
dtypes: float64(2)

The index has a period of 5 sec with some "holes", so i have created a TimeSeries with the same date range and period without holes:

<class 'pandas.tseries.index.DatetimeIndex'>
[2012-03-01 00:00:00, ..., 2012-06-16 23:59:55]
Length: 1866240, Freq: 5S, Timezone: None

Now i want to use this time series as index for the DataFrame above with the holes listed as NaN. How can I do this

Upvotes: 1

Views: 828

Answers (1)

Zelazny7
Zelazny7

Reputation: 40628

You can use the reindex() method and pass the padded index which you created.
The default fill_value is NaN

In [1]: ix1 = [0, 1, 2, 3, 4, 9]

In [2]: ix1
Out[2]: [0, 1, 2, 3, 4, 9]

In [3]: ix2 = range(10)

In [4]: ix2
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [5]: s = Series(ix1, index=ix1)

In [6]: s
Out[6]:
0    0
1    1
2    2
3    3
4    4
9    9

In [7]: s.reindex(ix2)
Out[7]:
0     0
1     1
2     2
3     3
4     4
5   NaN
6   NaN
7   NaN
8   NaN
9     9

In [8]: Series.reindex()?

Docstring:
Conform Series to new index with optional filling logic, placing
NA/NaN in locations having no value in the previous index. A new object
is produced unless the new index is equivalent to the current one and
copy=False

Upvotes: 4

Related Questions