Return 特定月份和年份的 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
我正在尝试创建一个函数,它将 return 仅与特定月份和年份相关的行:
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
定义函数
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
调用函数
return_data_month_year('Jan','2015')
预期输出:
order_date Type
2015-01-01 A
2015-01-27 E
我遇到错误(输出):
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00
您不必调用 month = pd.to_datetime(month).month()
和 year = pd.to_datetime(year).year()
。
还有 '%B'
return 的完整月份名称,例如。 January
。要 return 仅缩写 (Jan
, Feb
, ...), 使用 %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') )
打印:
order_date Type
0 2015-01-01 A
9 2015-01-27 E
我正在尝试创建一个函数,它将 return 仅与特定月份和年份相关的行:
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
定义函数
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
调用函数
return_data_month_year('Jan','2015')
预期输出:
order_date Type
2015-01-01 A
2015-01-27 E
我遇到错误(输出):
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00
您不必调用 month = pd.to_datetime(month).month()
和 year = pd.to_datetime(year).year()
。
还有 '%B'
return 的完整月份名称,例如。 January
。要 return 仅缩写 (Jan
, Feb
, ...), 使用 %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') )
打印:
order_date Type
0 2015-01-01 A
9 2015-01-27 E