LoginSignup
0
0

More than 1 year has passed since last update.

pandasで、年のない月日のみのデータを to_datetimeしたら1990年になってしまったときの対処法

Last updated at Posted at 2021-11-03

年の記載が無い、月日だけの日付データを to_datetime したら、全部1990年になってしまったので、年のデータを追加した方法を書いておきます。

サンプルデータ

年月日 平均気温(℃) 最高気温(℃) 最低気温(℃)
12月29日 7.3 12.4 1.9
12月30日 6.4 12.8 2.6
12月31日 3.0 7.1 -1.0
1月1日 2.9 9.7 -3.5
1月2日 3.6 9.9 -2.1
1月3日 3.0 7.5 -1.7
1月4日 4.9 11.2 -0.4

pandasで読み込む

import pandas as pd

df = pd.read_csv('sample_data.csv')
print(df)
print(df['年月日'])
print(df.iloc[:,0])   # df['年月日']と同じ


#      年月日  平均気温(℃)  最高気温(℃)  最低気温(℃)
# 0  12月29日      7.3     12.4      1.9
# 1  12月30日      6.4     12.8      2.6
# 2  12月31日      3.0      7.1     -1.0
# 3    1月1日      2.9      9.7     -3.5
# 4    1月2日      3.6      9.9     -2.1
# 5    1月3日      3.0      7.5     -1.7
# 6    1月4日      4.9     11.2     -0.4

# 0    12月29日
# 1    12月30日
# 2    12月31日
# 3      1月1日
# 4      1月2日
# 5      1月3日
# 6      1月4日
# Name: 年月日, dtype: object

# 0    12月29日
# 1    12月30日
# 2    12月31日
# 3      1月1日
# 4      1月2日
# 5      1月3日
# 6      1月4日
# Name: 年月日, dtype: object

そのまま to_datetimeすると

df['年月日'] = pd.to_datetime(df['年月日'], format='%m月%d日')
print(df)

#        年月日  平均気温(℃)  最高気温(℃)  最低気温(℃)
# 0 1900-12-29      7.3     12.4      1.9
# 1 1900-12-30      6.4     12.8      2.6
# 2 1900-12-31      3.0      7.1     -1.0
# 3 1900-01-01      2.9      9.7     -3.5
# 4 1900-01-02      3.6      9.9     -2.1
# 5 1900-01-03      3.0      7.5     -1.7
# 6 1900-01-04      4.9     11.2     -0.4

全部 1990年になってしまいました!
本当は、12月中は2020年に、1月以降は2021年にしたいんです。

対処法0

身も蓋もないのですが、小さなデータならExcelで 2020年2021年と書き加えちゃうのが一番簡単です😓。
後は format='%Y年%m月%d日'で完了...

対処法

pandasでやった一例です。

年を追加した年月日の配列を作って書き換える

new_dates = []

for i, value in enumerate(df['年月日']):
    if i < 3:
        new_dates.append('2020年' + value)
    else:
        new_dates.append('2021年' + value)

print(new_dates)

df['年月日'] = new_dates
print(df)

df['年月日'] = pd.to_datetime(df['年月日'], format='%Y年%m月%d日')
print(df)


# ['2020年12月29日', '2020年12月30日', '2020年12月31日', '2021年1月1日', '2021年1月2日', '2021年1月3日', '2021年1月4日']


#            年月日  平均気温(℃)  最高気温(℃)  最低気温(℃)
# 0  2020年12月29日      7.3     12.4      1.9
# 1  2020年12月30日      6.4     12.8      2.6
# 2  2020年12月31日      3.0      7.1     -1.0
# 3    2021年1月1日      2.9      9.7     -3.5
# 4    2021年1月2日      3.6      9.9     -2.1
# 5    2021年1月3日      3.0      7.5     -1.7
# 6    2021年1月4日      4.9     11.2     -0.4


#          年月日  平均気温(℃)  最高気温(℃)  最低気温(℃)
# 0 2020-12-29      7.3     12.4      1.9
# 1 2020-12-30      6.4     12.8      2.6
# 2 2020-12-31      3.0      7.1     -1.0
# 3 2021-01-01      2.9      9.7     -3.5
# 4 2021-01-02      3.6      9.9     -2.1
# 5 2021-01-03      3.0      7.5     -1.7
# 6 2021-01-04      4.9     11.2     -0.4

年月日の要素を1つずつ書き換える方法もあります。
ただし、次の例のように行名(index)や列名(columns)になっていると、1つずつの書き換えができないので、なるべく統一した書き方にしています。

インデックスの場合の書き換え

年月日がインデックスになっている場合の書き換えです。

import pandas as pd

df = pd.read_csv('sample_data.csv', index_col=0)
print(df)
print(df.index)

new_dates = []

for i, value in enumerate(df.index):
    if i < 3:
        new_dates.append('2020年' + value)
    else:
        new_dates.append('2021年' + value)


df.index = new_dates
df.index.name = '年月日'
print(df.index)

print(df)

df.index = pd.to_datetime(df.index, format='%Y年%m月%d日')
print(df)


#         平均気温(℃)  最高気温(℃)  最低気温(℃)
# 年月日                              
# 12月29日      7.3     12.4      1.9
# 12月30日      6.4     12.8      2.6
# 12月31日      3.0      7.1     -1.0
# 1月1日        2.9      9.7     -3.5
# 1月2日        3.6      9.9     -2.1
# 1月3日        3.0      7.5     -1.7
# 1月4日        4.9     11.2     -0.4


# Index(['12月29日', '12月30日', '12月31日', '1月1日', 
# '1月2日', '1月3日', '1月4日'], 
# dtype='object', name='年月日')


# Index(['2020年12月29日', '2020年12月30日', 
# '2020年12月31日', '2021年1月1日', '2021年1月2日', 
# '2021年1月3日', '2021年1月4日'], 
# dtype='object', name='年月日')


#              平均気温(℃)  最高気温(℃)  最低気温(℃)
# 年月日
# 2020年12月29日      7.3     12.4      1.9
# 2020年12月30日      6.4     12.8      2.6
# 2020年12月31日      3.0      7.1     -1.0
# 2021年1月1日        2.9      9.7     -3.5
# 2021年1月2日        3.6      9.9     -2.1
# 2021年1月3日        3.0      7.5     -1.7
# 2021年1月4日        4.9     11.2     -0.4


#             平均気温(℃)  最高気温(℃)  最低気温(℃)
# 年月日
# 2020-12-29      7.3     12.4      1.9
# 2020-12-30      6.4     12.8      2.6
# 2020-12-31      3.0      7.1     -1.0
# 2021-01-01      2.9      9.7     -3.5
# 2021-01-02      3.6      9.9     -2.1
# 2021-01-03      3.0      7.5     -1.7
# 2021-01-04      4.9     11.2     -0.4

列名(columns)の場合は、add_prefix(接頭辞を追加する)、add_suffix(接尾辞を追加する)メソッドがありますが、行名(index)にはありません。

 
思ってた以上に手間取りました。
もっといい方法がいろいろあるでしょうが、参考までに。

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