Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how set column as date index?

My data sets looks like:

Date        Value
1/1/1988    0.62
1/2/1988    0.64
1/3/1988    0.65
1/4/1988    0.66
1/5/1988    0.67
1/6/1988    0.66
1/7/1988    0.64
1/8/1988    0.66
1/9/1988    0.65
1/10/1988   0.65
1/11/1988   0.64
1/12/1988   0.66
1/13/1988   0.67
1/14/1988   0.66
1/15/1988   0.65
1/16/1988   0.64
1/17/1988   0.62
1/18/1988   0.64
1/19/1988   0.62
1/20/1988   0.62
1/21/1988   0.64
1/22/1988   0.62
1/23/1988   0.60

I used this code to read this data:

df.set_index(df['Date'], drop=False, append=False, inplace=False, verify_integrity=False).drop('Date', 1)

But the problem is the index is not in date format. So the question is how to set this column as date index?

like image 232
bikuser Avatar asked Jun 03 '16 09:06

bikuser


People also ask

How do you assign an index to a column?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.

How do I index a date in pandas?

date attribute outputs an Index object containing the date values present in each of the entries of the DatetimeIndex object. Example #1: Use DatetimeIndex. date attribute to find the date part of the DatetimeIndex object.

How do I change the index of a column in a data frame?

To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.


2 Answers

Your question lacked a proper explanation, but you can do the following:

In [75]:
# convert to datetime
df['Date'] = pd.to_datetime(df['Date'])
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 23 entries, 0 to 22
Data columns (total 2 columns):
Date     23 non-null datetime64[ns]
Value    23 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 448.0 bytes

In [76]:
# set the index
df.set_index('Date', inplace=True)
df.info()

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 23 entries, 1988-01-01 to 1988-01-23
Data columns (total 1 columns):
Value    23 non-null float64
dtypes: float64(1)
memory usage: 368.0 bytes

So here to_datetime will convert date strings to datetime dtype, set_index with param inplace=True is all you need,

like image 132
EdChum Avatar answered Oct 16 '22 21:10

EdChum


If you're loading data from a file, use parse_dates and index_col at load time, e.g.:

df = pd.read_csv('data.csv', parse_dates=['Date'], index_col=['Date'])

#             Value
# Date             
# 1988-01-01   0.62
# 1988-01-02   0.64
# ...
# 1988-01-23   0.60
df.index

# DatetimeIndex(['1988-01-01', '1988-01-02', ..., '1988-01-23'],
#               dtype='datetime64[ns]', name='Date', freq=None)

parse_dates is supported by most of the read_* methods:

  • read_csv
  • read_excel
  • read_fwf
  • read_html
  • read_sql
  • read_sql_query
  • read_sql_table
  • read_table
like image 40
tdy Avatar answered Oct 16 '22 19:10

tdy