Home > database >  TypeError wants Integers are getting String
TypeError wants Integers are getting String

Time:07-19

This code was originally made in a .ipynb file.

I am getting the TypeError: list indices must be integers or slices, not str can't seem to figure out how to fix this problem.

The result should be that the unix-timestamps in the dataframe get translated to (Year-Month) and the most recent date should be used as a file.

import numpy as np
import pandas as pd
import time
from datetime import datetime
import os
import re

df = pd.DataFrame()

files = os.listdir('input')

arr = [i for i in files if i.endswith('.csv') and 'export_' in i]
df = pd.DataFrame({'filename':arr})


res = []

# The code that gives the error.
for i in df.index:
    unix_code = re.findall('\d ', arr[i])
    for x in unix_code:
        "facturatie_vzs_"   datetime.utcfromtimestamp(unix_code[x]).strftime('%Y-%m')   ".csv"
        res.append(i)

Output of df

The error i am getting

CodePudding user response:

Use list comprehension with and for logical and with scalars (& is bitwise AND used in arrays) and then test substring by in operator, last array pass to DataFrame constructor:

files = os.listdir('input')

#for test
#files=['export_1656662723.csv', 'export_sss1654071237.csv']

arr = [i for i in files if i.endswith('.csv') and 'export_' in i]
df = pd.DataFrame({'filename':arr})

df['timestamp'] = pd.to_datetime(df['filename'].str.extract('(\d )',expand=False), unit='s')
print (df)
                   filename           timestamp
0     export_1656662723.csv 2022-07-01 08:05:23
1  export_sss1654071237.csv 2022-06-01 08:13:57
  • Related