简体   繁体   English

返回特定月份和年份的 df 行 python pandas OutOfBoundsDatetime:越界纳秒时间戳:1-01-01 00:00:00

[英]Return rows of df of particular month and year python pandas OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00

I am trying to create a function that will return rows pertaining only to particular month and year:我正在尝试创建一个函数,该函数将返回仅与特定月份和年份有关的行:

df df

order_date      Type
2015-01-01      A
2017-09-01      A
2016-12-19      C
2019-11-23      D
2018-10-29      B
2017-12-31      B
2015-11-30      A
2015-08-30      B
2015-09-24      D
2015-01-27      E

Defining function定义函数

def return_data_month_year(month, year):
    month = pd.to_datetime(month).month()
    year = pd.to_datetime(year).year()
    df = df[((df['order_date']).dt.strftime('%B') == month)&((df['order_date']).dt.strftime('%Y') == 
    year)]
    return df

Calling function调用函数

  return_data_month_year('Jan','2015')

Expected output:预期输出:

order_date      Type
2015-01-01      A
2015-01-27      E

I am getting error(Output):我收到错误(输出):

   OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00

You don't have to call month = pd.to_datetime(month).month() and year = pd.to_datetime(year).year() .您不必调用month = pd.to_datetime(month).month()year = pd.to_datetime(year).year()

Also '%B' returns full month name, eg. '%B'返回完整的月份名称,例如。 January . January To return only abbreviation ( Jan , Feb , ...), use %b :要仅返回缩写( JanFeb 、 ...),请使用%b

def return_data_month_year(df, month, year):
    return df[((df['order_date']).dt.strftime('%b') == month)&((df['order_date']).dt.strftime('%Y') == year)]

# to convert column 'order_date' to datetime:
df['order_date'] = pd.to_datetime( df['order_date'] )

print( return_data_month_year(df, 'Jan','2015') )

Prints:印刷:

  order_date Type
0 2015-01-01    A
9 2015-01-27    E

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 超出范围的纳秒级时间戳记:1-01-01 00:00:00 - Out of bounds nanosecond timestamp: 1-01-01 00:00:00 OutOfBoundsDatetime:越界纳秒时间戳:1-01-01 00:00:34 - OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34 越界纳秒时间戳:日期的 1-01-01 00:00:00 - Out of bounds nanosecond timestamp: 1-01-01 00:00:00 for dates 如何将Python中的01:00:00转换为integer? - How to convert 01:00:00 into integer in Python? pandas:无法使用Timestamp的这些索引器[2016-08-01 00:00:00]对DatetimeIndex进行位置索引 - pandas: cannot do positional indexing on DatetimeIndex with these indexers [2016-08-01 00:00:00] of Timestamp “2006-04-03 08:00:00+01:00”日期时间戳 object 中的“+01:00”是什么? - What is the "+01:00" in "2006-04-03 08:00:00+01:00" datetime timestamp object? Postgres时间戳不接受2017-01-01T23:00:00-00:00 - Postgres timestamp not accepting 2017-01-01T23:00:00-00:00 OutOfBoundsDatetime:越界纳秒时间戳 - OutOfBoundsDatetime: Out of bounds nanosecond timestamp 使用 pandas 将 2020-01-22 00:00:00+00:00 转换为 2020-01-22 - convert 2020-01-22 00:00:00+00:00 into 2020-01-22 using pandas 如何在 python 中将这种格式 '2020-01-01 00:00:00+00:00' 的 str 转换为日期时间? - How to convert a str like this format '2020-01-01 00:00:00+00:00' to datetime in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM