Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a pandas DataFrame into a TimeSeries?

I am looking for a way to convert a DataFrame to a TimeSeries without splitting the index and value columns. Any ideas? Thanks.

In [20]: import pandas as pd  In [21]: import numpy as np  In [22]: dates = pd.date_range('20130101',periods=6)  In [23]: df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))  In [24]: df Out[24]:                    A         B         C         D 2013-01-01 -0.119230  1.892838  0.843414 -0.482739 2013-01-02  1.204884 -0.942299 -0.521808  0.446309 2013-01-03  1.899832  0.460871 -1.491727 -0.647614 2013-01-04  1.126043  0.818145  0.159674 -1.490958 2013-01-05  0.113360  0.190421 -0.618656  0.976943 2013-01-06 -0.537863 -0.078802  0.197864 -1.414924  In [25]: pd.Series(df) Out[25]: 0    A 1    B 2    C 3    D dtype: object 
like image 958
morgan Avatar asked May 29 '13 20:05

morgan


People also ask

How do you convert a DataFrame to a time series in Python?

to_datetime() Function helps in converting a date string to a python date object. So, it can be utilized for converting a series of date strings to a time series.

How do you convert a DataFrame to a data series?

To convert the last or specific column of the Pandas dataframe to series, use the integer-location-based index in the df. iloc[:,0] . For example, we want to convert the third or last column of the given data from Pandas dataframe to series. In this case, the following code example will help us.

How do you convert DataFrame values to integers?

Convert Column to int (Integer)Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.

Is pandas good for time series?

pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.


1 Answers

I know this is late to the game here but a few points.

Whether or not a DataFrame is considered a TimeSeries is the type of index. In your case, your index is already a TimeSeries, so you are good to go. For more information on all the cool slicing you can do with a the pd.timeseries index, take a look at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#datetime-indexing

Now, others might arrive here because they have a column 'DateTime' that they want to make an index, in which case the answer is simple

ts = df.set_index('DateTime') 
like image 69
EngineeredE Avatar answered Sep 20 '22 19:09

EngineeredE