python:How to Get and Convert Dates(numpy.datetime64) with Pandas

Published on:
Last updated:

This post is also available in: Japanese

This is a note about how to get or convert date(numpy.datetime64 format) with the python library pandas.
Useful when extracting year, month, day, ...etc. from numpy.datetime64 format.

Convert datetime64 created from empty numpy array using pandas

The below sample code is that when you want to use pandas to get or convert dates from numpy.datetime64 format created from an empty numpy array(np.empty).
In the following code, pd.to_datetime() will be the pandas Timestamp type, so you can apply the pandas method.
Please refer to the following site for details.
pandas.Timestamp — pandas 0.23.4 documentation

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

date_df = np.empty(2, dtype='datetime64[ns]')
date_df[0] = np.datetime64('2016-09-29T00:00:00.000000000')
date_df[1] = np.datetime64('2016-09-28T00:00:00.000000000')
# Convert data by pandas
date_df = pd.to_datetime(date_df)

# print
print type(date_df) # <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
print type(date_df[0]) # <class 'pandas._libs.tslib.Timestamp'>
print date_df  # DatetimeIndex(['2016-09-29', '2016-09-28'], dtype='datetime64[ns]', freq=None)
print str(date_df[0].day) # 29
print str(date_df[0].month) # 9
print str(date_df[0].year) # 2016

Convert datetime64 created from empty numpy array using astype

The below sample code is that when you want to use pandas to get or convert dates from numpy.datetime64 format by using astype created from an empty numpy array(np.empty).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

date_df = np.empty(2, dtype='datetime64[ns]')
date_df[0] = np.datetime64('2016-09-29T00:00:00.000000000')
date_df[1] = np.datetime64('2016-09-28T00:00:00.000000000')

# print
print type(date_df[0]) # <type 'numpy.datetime64'>
print date_df  # ['2016-09-29T00:00:00.000000000' '2016-09-28T00:00:00.000000000']
print date_df.astype('M8[D]') # ['2016-09-29' '2016-09-28' ]
print date_df.astype('M8[M]') # ['2016-09' '2016-09']
print date_df.astype('M8[Y]') # ['2016' '2016']

Convert datetime64 from CSV date data by using astype

The CSV data used in the sample is stock price data of Yahoo Finance US.
The below sample code is that when you want to use pandas to get or convert dates from numpy.datetime64 format by using astype from CSV date data.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd

# Read CSV
df = pd.read_csv("data/SMPLE.csv",index_col=0, parse_dates=True)
# Get index(date) values
date_df = df.index.get_values()
# print
print type(date_df[0]) # <type 'numpy.datetime64'>
print date_df  # ['2016-09-29T00:00:00.000000000' '2016-09-28T00:00:00.000000000']
print date_df.astype('M8[D]') # ['2016-09-29' '2016-09-28' ]
print date_df.astype('M8[M]') # ['2016-09' '2016-09']

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.