0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pandasで日付処理する。

Last updated at Posted at 2025-02-02

はじめに

エクセルファイルをpandasでデータフレームにしたとき、日付が、タイムスタンプになっている時と、文字列になっている時がある。処理しやすくするようにフォーマットを変換する方法をまとめる。

エクセルファイルの例

A,B列は日付フォーマット、C,Dは文字列フォーマット。ファイル名は「pandas_day.xlsx」とする。

image.png

各列のフォーマット確認

日付フォーマットはtimestampで文字列フォーマットはstrでデータフレームになる。

import pandas as pd

df = pd.read_excel('pandas_day.xlsx',)

# 値
print(f"{df}")

#            A          B         C          D
# 0 2020-02-01 2020-02-01  2020/2/1  2020年2月1日
# 1 2020-02-02 2020-02-02  2020/2/2  2020年2月2日
# 2 2020-02-03 2020-02-03  2020/2/3  2020年2月3日
# 3 2025-02-01 2025-02-01  2025/2/1  2025年2月1日
# 4 2025-02-02 2025-02-02  2025/2/2  2025年2月2日
# 5 2025-02-03 2025-02-03  2025/2/3  2025年2月3日
# 6 2025-03-01 2025-03-01  2025/3/1  2025年3月1日
# 7 2025-03-02 2025-03-02  2025/3/2  2025年3月2日
# 8 2025-03-03 2025-03-03  2025/3/3  2025年3月3日

# type確認
print(f"{type(df['A'][0])=}\n{type(df['B'][0])=}\n{type(df['C'][0])=}\n{type(df['D'][0])=}")

# type(df['A'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# type(df['B'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# type(df['C'][0])=<class 'str'>
# type(df['D'][0])=<class 'str'>


Timestampsへ変換する

pd.to_datetime()で変換できる。format=で文字列のフォーマットを指定して変換する。

import pandas as pd

df = pd.read_excel('pandas_day.xlsx',)

# 日時変更
df['C']=pd.to_datetime(df['C'], format='%Y/%m/%d')
df['D'] = pd.to_datetime(df['D'], format='%Y年%m月%d日')

# 値
print(f"{df}")

#            A          B         C          D
# 0 2020-02-01 2020-02-01 2020-02-01 2020-02-01
# 1 2020-02-02 2020-02-02 2020-02-02 2020-02-02
# 2 2020-02-03 2020-02-03 2020-02-03 2020-02-03
# 3 2025-02-01 2025-02-01 2025-02-01 2025-02-01
# 4 2025-02-02 2025-02-02 2025-02-02 2025-02-02
# 5 2025-02-03 2025-02-03 2025-02-03 2025-02-03
# 6 2025-03-01 2025-03-01 2025-03-01 2025-03-01
# 7 2025-03-02 2025-03-02 2025-03-02 2025-03-02
# 8 2025-03-03 2025-03-03 2025-03-03 2025-03-03

# type確認
print(f"{type(df['A'][0])=}\n{type(df['B'][0])=}\n{type(df['C'][0])=}\n{type(df['D'][0])=}")

# type(df['A'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# type(df['B'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# type(df['C'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# type(df['D'][0])=<class 'pandas._libs.tslibs.timestamps.Timestamp'>

日付で行フィルタする

dt.yeardt.monthdt.dayで日付をフィルタできる。

import pandas as pd

df = pd.read_excel('pandas_day.xlsx',)

# 2020年だけフィルタ
df_2020=df[df['A'].dt.year == 2020]
# 値
print(f"{df_2020}")

#            A          B          C          D
# 0 2020-02-01 2020-02-01 2020-02-01 2020-02-01
# 1 2020-02-02 2020-02-02 2020-02-02 2020-02-02
# 2 2020-02-03 2020-02-03 2020-02-03 2020-02-03


# 2月だけフィルタ
df_2=df[df['A'].dt.month == 2]
# 値
print(f"{df_2}")

#            A          B          C          D
# 0 2020-02-01 2020-02-01 2020-02-01 2020-02-01
# 1 2020-02-02 2020-02-02 2020-02-02 2020-02-02
# 2 2020-02-03 2020-02-03 2020-02-03 2020-02-03
# 3 2025-02-01 2025-02-01 2025-02-01 2025-02-01
# 4 2025-02-02 2025-02-02 2025-02-02 2025-02-02
# 5 2025-02-03 2025-02-03 2025-02-03 2025-02-03

フォーマット指定をして文字列に変換する

dt.strftime()でフォーマットを指定して文字列に変換できる。

import pandas as pd

df = pd.read_excel('pandas_day.xlsx',)

# 日付を文字列に変更
df['A']=df['A'].dt.strftime('%Y年%m月%d日')
df['B']=df['B'].dt.strftime('%Y年%m月%d日')
df['C']=df['C'].dt.strftime('%Y年%m月%d日')
df['D']=df['D'].dt.strftime('%Y年%m月%d日')
# 値
print(f"{df}")

#              A            B            C            D
# 0  2020年02月01日  2020年02月01日  2020年02月01日  2020年02月01日
# 1  2020年02月02日  2020年02月02日  2020年02月02日  2020年02月02日
# 2  2020年02月03日  2020年02月03日  2020年02月03日  2020年02月03日
# 3  2025年02月01日  2025年02月01日  2025年02月01日  2025年02月01日
# 4  2025年02月02日  2025年02月02日  2025年02月02日  2025年02月02日
# 5  2025年02月03日  2025年02月03日  2025年02月03日  2025年02月03日
# 6  2025年03月01日  2025年03月01日  2025年03月01日  2025年03月01日
# 7  2025年03月02日  2025年03月02日  2025年03月02日  2025年03月02日
# 8  2025年03月03日  2025年03月03日  2025年03月03日  2025年03月03日

# type確認
print(f"{type(df['A'][0])=}\n{type(df['B'][0])=}\n{type(df['C'][0])=}\n{type(df['D'][0])=}")

# type(df['A'][0])=<class 'str'>
# type(df['B'][0])=<class 'str'>
# type(df['C'][0])=<class 'str'>
# type(df['D'][0])=<class 'str'>

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?