Вы получаете ошибку, потому что, если вы распечатаете data[0]
, она не будет разделена так, как вы ожидаете. На самом деле это совсем не расщепление. Вот как вы можете исправить свой код:
data = ["1/3/2012 16:00:00 Missing_1",
"1/4/2012 16:00:00 27.47",
"1/5/2012 16:00:00 27.728",
"1/6/2012 16:00:00 28.19",
"1/9/2012 16:00:00 28.1",
"1/10/2012 16:00:00 28.15",
"12/13/2012 16:00:00 27.52",
"12/14/2012 16:00:00 Missing_19",
"12/17/2012 16:00:00 27.215",
"12/18/2012 16:00:00 27.63",
"12/19/2012 16:00:00 27.73",
"12/20/2012 16:00:00 Missing_20",
"12/21/2012 16:00:00 27.49",
"12/24/2012 13:00:00 27.25",
"12/26/2012 16:00:00 27.2",
"12/27/2012 16:00:00 27.09",
"12/28/2012 16:00:00 26.9",
"12/31/2012 16:00:00 26.77"]
# Standardize the formatting...
data = [i.split() for i in data]
data = ["{} {} {}".format(i[0].strip(), i[1].strip(), i[2].strip()) for i in data]
date_list = []
mrc_list = []
for i in data:
# split on four spaces instead of tab (special character)
data = i.split(' ')
days_of_data = datetime.strptime(data[0], '%m/%d/%Y %H:%M:%S')
date_list.append(days_of_data)
try:
mrc_list.append(float(data[1]))
except:
mrc_list.append(np.nan)
pass
mrc_df = pd.Series(mrc_list, index=date_list)
mrc_df.index.name = 'Date'
print(mrc_df)
Более компактный способ добиться этого:
from datetime import datetime
import pandas as pd
import numpy as np
from io import StringIO
data = ["1/3/2012 16:00:00 Missing_1",
"1/4/2012 16:00:00 27.47",
"1/5/2012 16:00:00 27.728",
"1/6/2012 16:00:00 28.19",
"1/9/2012 16:00:00 28.1",
"1/10/2012 16:00:00 28.15",
"12/13/2012 16:00:00 27.52",
"12/14/2012 16:00:00 Missing_19",
"12/17/2012 16:00:00 27.215",
"12/18/2012 16:00:00 27.63",
"12/19/2012 16:00:00 27.73",
"12/20/2012 16:00:00 Missing_20",
"12/21/2012 16:00:00 27.49",
"12/24/2012 13:00:00 27.25",
"12/26/2012 16:00:00 27.2",
"12/27/2012 16:00:00 27.09",
"12/28/2012 16:00:00 26.9",
"12/31/2012 16:00:00 26.77"]
data = [i.split() for i in data]
data = ["{} {} {}".format(i[0].strip(), i[1].strip(), i[2].strip()) for i in data]
data = ["Date Val"] + data
mrc_df = pd.read_csv(StringIO("\n".join(data)), sep="\s\s+", engine='python')
mrc_df['Val'] = pd.to_numeric(mrc_df['Val'], errors='coerce')
mrc_df['Date'] = pd.to_datetime(mrc_df['Date'])
mrc_df.set_index('Date', inplace=True)
mrc_df.index.name = 'Date'