2

Suppose I have a csv with a timestamp but the format is not defined. It can be of any format with any separator like -

mm/dd/yyyy hh:mm or dd/mm/yyyy hh:mm:ss or mm-dd-yyyy hh:mm or dd-mm-yyyy hh:mm:ss or just like that.

I am trying to parse dates of any format.

Here:

dateparse = lambda dates: datetime.strptime(dates, '%m/%d/%Y %H:%M')

We have defined to parse dates in this format: %m/%d/%Y %H:%M

If anyone can give any valuable suggestion then it will be helpful.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Dheeraj
  • 1,102
  • 3
  • 14
  • 29
  • 1
    Possible duplicate of [datetime from string in Python, best-guessing string format](http://stackoverflow.com/questions/9507648/datetime-from-string-in-python-best-guessing-string-format) – Mike Scotty Apr 11 '17 at 06:55
  • Are the date formats within an individual csv file in a consistent format? If not then it is not possible to distinguish between, for example, 1st February and 2nd January. – heroworkshop Apr 11 '17 at 07:12

1 Answers1

1

pandas.read_csv has an infer_datetime_format parameter:

infer_datetime_format : boolean, default False

If True and parse_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x.

Community
  • 1
  • 1
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135