0

I am looping through folder to create json objects of all the excel-files there. This works fine for most of the files besides some that comes with a different dateformat. Some comes with an empty line which is okay. The script worked. However, when it comes as a new type of dateformat it fails with error:

TypeError: argument of type 'Timestamp' is not iterable

  datelist =[]
  for col in data.columns:
      if col == str("PriceDate"):
          for val in data[col]:            
              if "\n" in val:
                  val = "".join(val.split())           
              val =  datetime.strptime(val, '%Y-%m-%d')
              val = val.strftime('%d/%m/%Y')
              datelist.append(val)

How can I solve this?

Excel data example: enter image description here

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
andeng
  • 1
  • 1
  • 2
    How does your `data` object look like? You should provide a minimal reproducible example here. Please check https://stackoverflow.com/help/minimal-reproducible-example. – Pubudu Sitinamaluwa Aug 24 '20 at 14:18
  • I am using pandas to read through an excel file. The column consists the same date for around 100 rows. data = pd.read_excel(filename) – andeng Aug 24 '20 at 14:24
  • Can you provide a small sample of a few lines form your exel? – Pubudu Sitinamaluwa Aug 24 '20 at 14:27
  • Look at the attached image at the original post! – andeng Aug 24 '20 at 14:33
  • why do you need the loop construct? you could simply convert to datetime using `pd.to_datetime(data["PriceDate"])` [docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html). – FObersteiner Aug 24 '20 at 14:39
  • Does this answer your question? [How to change the datetime format in pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – FObersteiner Aug 24 '20 at 14:42
  • This does not seem to work. I might be constructing it wrong, but I used a double loop construct to first find the right column and then loop through that column and replace all the dates as string. And fix format issues when it is delivered from our source – andeng Aug 24 '20 at 14:57

0 Answers0