Home>

I am currently analyzing data with python's jupyter notebook.
I read the data group including the column in which the date was entered like "2018/12/31" in the csv file with the to_csv function. And I tried to convert that column to a separate row by year, month, and day, but it doesn't work. In particular

time = datetime.strptime (set ["date"], "% Y /% m /% d")

I said

TypeError: strptime () argument 1 must be str, not Series


I got an error. I was able to speculate that the content of the error might mean that Series cannot be converted by strptime (), but I couldn't think of what means could be used instead.

Thanks for your answer.

  • Answer # 1

    I read the data group including the column in which the date was entered like "2018/12/31" in the csv file with the to_csv function.

    of pandasread_csvI wonder if it's okay to read it in.to_csvIs for writing.

    When reading, Optional argumentsparse_datesIf you specify the date column with, you will get a datetime64 [ns] type column with each element of type TimeStamp. The value delimiter---But/But OK.

    The rest is prepared for the dt accessordt.dayTake out each value using etc. and make a new column to complete.

    df = pd.read_csv ('hogehoge.csv', parse_dates = ['date'])
    df ['year'] = df ['year/month/day'] .dt.year
    df ['month'] = df ['year/month/day'] .dt.month
    df ['day'] = df ['date']. dt.day
    print (df)

  • Answer # 2

    And that column by year, month, date

    I think you can get the year, month, and day below.

    set ["year/month/day"] = pd.to_datetime (set ["year/month/day"])
    set ["year"] = set ["year/month/day"] .dt.year
    set ["month"] = set ["year/month/day"] .dt.month
    set ["day"] = set ["year/month/day"] .dt.day