EP1986
EP1986

Reputation: 883

create timestemps for given data

I have a pandas DataFrame df like :

Typ1 Typ2 Typ3

10 50 60

20 100 80

75 80 20

... and so an (there are 8760 rows for 8760 hours in one year). That means, that the first row is the value for 2012-01-01 00:00:00, the second row show the value for 2012-01-01 00:01:00 and so on.

I have two points:

FIRST:

Now, I want to create Timestemps from the first january to the 31th december of the specific year and put it besides the values. How could I managed that? I try something with pd.date_range(), but there I only have the date, not the time.

SECOND:

Finaly, from the timestemps, I need the weekday of the specific hour in the discussed year, or of the Timestemp from the step FIRST. But I don`t know, how I can 'import' the information, what weekday the hour x, y etc. was?!

At the end, my DataFrame should look like (here for year 2012):

weekday Typ1 Typ2 Typ3

sunday 10 50 60

sunday 20 100 80

sunday 75 80 20

...and so on up to 31th december 2012 'monday' for the 8760th hour. (for the year 2013 it would start with thuesday)

Upvotes: 0

Views: 123

Answers (1)

user3820991
user3820991

Reputation: 2630

I believe what you want is

pd.date_range('2012-01-01','2012-12-31', freq='T')

For the different choices of frequencies, see here http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases .

To attach this to your dataframe:

df['Timestamp'] = pd.date_range('2012-01-01','2012-12-31', freq='T')

and if you want to use it as index

df.set_index('Timestamp', inplace=True)

With regard to your second question. You can retrieve the weekday from the Timestamp.

x = pd.date_range('2012-01-01','2012-12-31', freq='T')
x.weekday

will then give you an array with values 0,1,...,6 standing for the respective weekday.

Upvotes: 1

Related Questions