【问题标题】:How do I add a year and month columns to my pandas dataframe using Unix timestamps?如何使用 Unix 时间戳向我的 pandas 数据框添加年份和月份列?
【发布时间】:2023-04-04 07:04:01
【问题描述】:

我的 Pandas 数据框中有一列名为“date”,其中包含 unix 时间戳 (int64)。我正在尝试遍历整个帧并从时间戳中提取月份和年份并将它们添加到我的数据框中。有了月份和年份后,我希望能够创建掩码,以便可以根据月份和年份将新数据框保存到 CSV 以下是我编写的代码:

# import useful libraries
from datetime import datetime
import pandas as pd

# read csv as dataframe
df=pd.read_csv('./ct.csv')

# function to get year
def get_year(x):
    return datetime.fromtimestamp(x).strftime("%Y")

# function to get month
def get_month(x):
    return datetime.fromtimestamp(x).strftime("%m")

# add month and year to new dataframe columns
df['year'] = df['date'].apply(get_year)
df['month'] = df['date'].apply(get_month)

# set the beginning and end date for mask
beginning = datetime(2002, 1, 1)
end = datetime(2003, 1, 1)

# get datetime from timestamp
def to_datetime(x):
    print(x)
    return datetime.fromtimestamp(x)

# create datetime series
df['datetime'] = df['date'].apply(to_datetime)

# create dataframe mask
msk = (df['datetime'] > beginning) & (df['datetime'] < end)

# apply mask
df_range = df[msk]

# write dataframe to csv
df_range.to_csv('ct_2002.csv', index=False)

尝试运行时出现以下错误:

    runfile('C:/Users/x/Desktop/Wine/daterange.py', wdir='C:/Users/x/Desktop/Wine')
Traceback (most recent call last):

  File "C:\Users\x\Desktop\Wine\daterange.py", line 17, in <module>
    df['year'] = df['date'].apply(get_year)

  File "C:\Users\x\Anaconda3\lib\site-packages\pandas\core\series.py", line 3848, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)

  File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer

  File "C:\Users\x\Desktop\Wine\daterange.py", line 10, in get_year
    return datetime.fromtimestamp(x).strftime("%Y")

OSError: [Errno 22] Invalid argument

任何帮助将不胜感激。

【问题讨论】:

  • 你见过this issue吗?当脚本在您的一个时间戳上调用 get_year 时,它似乎被挂断了。也许尝试在从数据中提取的单个时间戳上运行它,否则检查该列是否有格式不正确的戳
  • 您的输入数据是什么样的?我一般,我建议看看熊猫内置的日期时间方法

标签: python pandas datetime timestamp


【解决方案1】:

我不确定您的数据框是什么样的,但这是我将如何进行的。正如 MrFuppes 指出的那样,Pandas 具有内置的日期时间功能。

import pandas as pd

df = pd.read_csv('./ct.csv')

#Convert unix timestamps to datetime.
df['datetime'] = pd.to_datetime(df['date'],unit='s')

#Set the 'datetime' field to be the index.
df.set_index('datetime',inplace=True)

#Extract the month from the index.
df['month'] = df.index.month

#Extract the year from the index.
df['year'] = df.index.year

#Apply a temporal slice.
df = df['2002-1-1':'2003-1-1']

df.to_csv('ct_2002.csv')

如果这不能解决您的问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2020-10-10
    • 2021-08-31
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多