0

Starting with this code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

vento=pd.read_csv('dados_tpm.txt')
vento.rename(columns={'Dia_Mes_Ano_Hora_Minuto': 'Data'})
vento.set_index('Data')

The dataframe is something like this:

    Data                Vel Dir
    2016-07-12 16:26:00 2.4  21.0
    2016-07-12 16:27:00 1.7  17.8
    2016-07-12 16:29:00 14.3 14.9

The index is already in datetime. The objective is to substitute the index above to a new index created by this code below and keep all values in vento columns:

vento3 = pd.DataFrame({'Data':pd.date_range(start='2016-07-12 16:17:00',end='2017-04-30 22:34:00',freq='1Min')})
vento3.set_index('Data')

Getting this index like:

Data
2016-07-12 16:26:00
2016-07-12 16:27:00
2016-07-12 16:28:00
2016-07-12 16:29:00

Desired output:

Data                Vel Dir
2016-07-12 16:26:00 2.4  21.0
2016-07-12 16:27:00 1.7  17.8
2016-07-12 16:28:00 NaN  NaN
2016-07-12 16:29:00 14.3 14.9

Would be thankful if someone could help.

3
  • 1
    You should be able to just do vento.reindex(pd.date_range(start='2016-07-12 16:17:00',end='2017-04-30 22:34:00',freq='1Min')) Commented May 8, 2017 at 14:31
  • If i use this got i got this error: ValueError: cannot reindex from a duplicate axis Commented May 8, 2017 at 14:33
  • I get the same error. I wonder why that is. Commented May 8, 2017 at 19:41

1 Answer 1

2

Source DF:

In [23]: df
Out[23]:
                      Vel   Dir
Data
2016-07-12 16:26:00   2.4  21.0
2016-07-12 16:27:00   1.7  17.8
2016-07-12 16:29:00  14.3  14.9

Solution:

In [22]: df.resample('T').mean().reset_index()
Out[22]:
                 Data   Vel   Dir
0 2016-07-12 16:26:00   2.4  21.0
1 2016-07-12 16:27:00   1.7  17.8
2 2016-07-12 16:28:00   NaN   NaN
3 2016-07-12 16:29:00  14.3  14.9
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't know that i could reset index while using resampling function, thanks a lot.
@LeonardoFerreira, glad I could help :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.