pandas parse_dates code example

Example 1: date parser python pandas

mydateparser = lambda x: pd.datetime.strptime(x, "%Y %m %d %H:%M:%S")
df = pd.read_csv("file.csv", sep='\t', names=['date_column', 'other_column'], parse_dates=['date_column'], date_parser=mydateparser)

Example 2: date parser python pandas

format='%d-%b-%Y'

Example 3: pandas read csv

df = pd.read_csv('data.csv')

Example 4: pandas read csv

import pandas as pd
cereal_df = pd.read_csv("/tmp/tmp07wuam09/data/cereal.csv")
cereal_df2 = pd.read_csv("data/cereal.csv")

# Are they the same?
print(pd.DataFrame.equals(cereal_df, cereal_df2))

Example 5: date parser python pandas

import pandas as pd

values = {'dates':  ['02-Sep-2019','13-Sep-2019','21-Sep-2019'],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame(values, columns = ['dates','status'])

df['dates'] = pd.to_datetime(df['dates'], format='%d-%b-%Y')

print (df)
print (df.dtypes)

Example 6: pandas read csv python

pd.read_csv('data.csv')  # doctest: +SKIP

Tags:

Go Example