David
David

Reputation: 1267

Using asfreq to resample a pandas dataframe

EDIT: I had made a mistake and my index was starting at 00:00:00, not at 06:00:00 (see below). So this question is spurious, but of course Wen's solution is correct.

I have a dataframe whose index goes like this:

2017-11-01 06:00:00
2017-11-02 06:00:00 
2017-11-03 06:00:00
...

and so on. But I have the suspicion there're missing entries, for instance, index for 2017-11-04 06:00:00 could be missing. I have used

df = df.asfreq(freq="1D")

to fill with NaN the missing values, but it creates a new index that doesn't take into consideration the hours, it goes 2017-11-01, 2017-11-02 and so on, so the values in the adjacent column are all NaN!

How can I fix this? I don't see any option in asfreq that can solve it. Perhaps other tool? Thanks in advance.

Upvotes: 0

Views: 371

Answers (1)

BENY
BENY

Reputation: 323276

It work find on my side

l=[
'2017-11-01 06:00:00',
'2017-11-03 06:00:00']

ts = pd.Series(np.random.randn(len(l)), index=l)
ts.index=pd.to_datetime(ts.index)
ts.asfreq(freq="D")
Out[745]: 
2017-11-01 06:00:00   -0.467919
2017-11-02 06:00:00         NaN
2017-11-03 06:00:00    1.610024
Freq: D, dtype: float64

Upvotes: 1

Related Questions