简体   繁体   中英

Pandas convert partial column Index to Datetime

DataFrame below contains housing price dataset from 1996 to 2016.

Other than the first 6 columns, other columns need to be converted to Datetime type.

DataFrame 的房价数据集列

I tried to run the following code:

HousingPrice.columns[6:] = pd.to_datetime(HousingPrice.columns[6:])

but I got the error:

TypeError: Index does not support mutable operations

I wish to convert some columns in the columns Index to Datetime type, but not all columns.

The pandas index is immutable, so you can't do that.

However, you can access and modify the column index with array , see doc here .

HousingPrice.columns.array[6:] = pd.to_datetime(HousingPrice.columns[6:])

should work.

Note that this would change the column index only. In order to convert the columns values, you can do this :

date_cols = HousingPrice.columns[6:]
HousingPrice[date_cols] = HousingPrice[date_cols].apply(pd.to_datetime, errors='coerce', axis=1)

EDIT

Illustrated example:

data = {'0ther_col': [1,2,3], '1996-04': ['1996-04','1996-05','1996-06'], '1995-05':['1996-02','1996-08','1996-10']}

print('ORIGINAL DATAFRAME')
df = pd.DataFrame.from_records(data)
print(df)

print("\nDATE COLUMNS")

date_cols = df.columns[-2:]
print(df.dtypes)

print('\nCASTING DATE COLUMNS TO DATETIME')

df[date_cols] = df[date_cols].apply(pd.to_datetime, errors='coerce', axis=1)
print(df.dtypes)

print('\nCASTING DATE COLUMN INDEXES TO DATETIME')

print("OLD INDEX -", df.columns)
df.columns.array[-2:] = pd.to_datetime(df[date_cols].columns)
print("NEW INDEX -",df.columns)

print('\nFINAL DATAFRAME')
print(df)

yields:

ORIGINAL DATAFRAME
   0ther_col  1995-05  1996-04
0          1  1996-02  1996-04
1          2  1996-08  1996-05
2          3  1996-10  1996-06

DATE COLUMNS
0ther_col     int64
1995-05      object
1996-04      object
dtype: object

CASTING DATE COLUMNS TO DATETIME
0ther_col             int64
1995-05      datetime64[ns]
1996-04      datetime64[ns]
dtype: object

CASTING DATE COLUMN INDEXES TO DATETIME
OLD INDEX - Index(['0ther_col', '1995-05', '1996-04'], dtype='object')
NEW INDEX - Index(['0ther_col', 1995-05-01 00:00:00, 1996-04-01 00:00:00], dtype='object')

FINAL DATAFRAME
   0ther_col 1995-05-01 00:00:00 1996-04-01 00:00:00
0          1          1996-02-01          1996-04-01
1          2          1996-08-01          1996-05-01
2          3          1996-10-01          1996-06-01

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM