0

I have a data frame consisting of hourly wind speed measurements for the year 2012 for different locations as seen below:

enter image description here

I was able to change the index to datetime format for just one location using the code:

dfn=df1_s.reset_index(drop=True)
import datetime
dfn['datetime']=pd.to_datetime(dfn.index,
origin=pd.Timestamp('2012-01-01 00:00:00'), unit= 'H')

When using the same code over the entire dataframe, i obtain the error: cannot convert input with unit 'h'. This is probably because there are way to much data than the number of years to be represented by 'hours', i am not sure. Nevertheless, it works when i use units in minutes i.e. units='m'.

What i want to do is to set the datetime in such a way that it repeats itself after every 8784 hours i.e. having the same replicate datetime format for each location on the same dataframe as seen in the image below(expected results produced on excel).

enter image description here

When trying the following, all i obtained was a column with a series on NaNs:

import pdb, random

dates = pd.date_range('2012-01-01', '2013-01-01', freq='H')
data  = [int(1000*random.random()) for i in range(len(dates))]
dfn['cum_data'] = pd.Series(data, index=dates)

Can you please direct me on how to go about this?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
vichel
  • 1
  • 2
  • 1
    You obtained a column of `np.nan` because the indices of your `pd.Series(data, index=dates)` and `dfn` do not match. – hoomant Aug 17 '20 at 14:14
  • Please provide a sample of your DataFrame (e.g. `dfn.head()`). – hoomant Aug 17 '20 at 14:15
  • you cannot have different 'formats' in a column of dtype datetime; that column would have to be of dtype string (object). – FObersteiner Aug 17 '20 at 14:43
  • @hoomant i just provided an edit with the sample dataframe as a link. unfortunately i am not able to embed pictures on this platform yet. – vichel Aug 17 '20 at 14:50
  • Wlcome vichel, please see this [answer](https://stackoverflow.com/a/20159305/6692898) that shows how to provide pandas sample data. The image link you provided does not show any `datetime` column thus it is hard to tell why you get that error trying to convert with 'H'... also I don't get why you would choose to parse with minutes instead of hours, can you explain the origin of that data? – RichieV Aug 17 '20 at 15:06

0 Answers0