J'essaie de créer un bloc de données à partir d'un ensemble de données séparés par des espaces. Certaines valeurs de la 3e colonne sont manquantes et elles sont étiquetées Missing_x. J'essaie de remplacer ces valeurs par np.nan mais cela me renvoie une ValueError.

from datetime import datetime
import pandas as pd
import numpy as np

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"]

date_list = []
mrc_list = []

for i in data:
    data = i.split('\t')
    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)

Voici l'erreur:

Traceback (most recent call last):
  File "/home/onur/Documents/code-signal/mercury.py", line 37, in <module>
    days = datetime.strptime(data_list[0], '%m/%d/%Y %H:%M:%S')
  File "/home/onur/anaconda3/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/home/onur/anaconda3/lib/python3.7/_strptime.py", line 362, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains:    Missing_1

Je comprends l'erreur. Je ne comprends tout simplement pas pourquoi ma façon de l'aborder ne fonctionne pas.

0
oo92 12 févr. 2020 à 04:00

1 réponse

Meilleure réponse

Vous obtenez l'erreur car si vous imprimez data[0], il ne se divise pas comme prévu. Il ne se sépare pas du tout en fait. Voici comment vous pouvez corriger votre code :

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)

Un moyen plus compact d'y parvenir est :

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'
1
cosmic_inquiry 12 févr. 2020 à 01:27