- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个包含两列的数据框; 销售额
和日期
。
dataset.head(10)
Date Sales
0 2015-01-02 34988.0
1 2015-01-03 32809.0
2 2015-01-05 9802.0
3 2015-01-06 15124.0
4 2015-01-07 13553.0
5 2015-01-08 14574.0
6 2015-01-09 20836.0
7 2015-01-10 28825.0
8 2015-01-12 6938.0
9 2015-01-13 11790.0
我想将 Date
列从 yyyy-mm-dd
(例如 2015-06-01
)转换为 yyyy -ww
(例如 2015-23
),所以我运行以下代码:
dataset["Date"] = pd.to_datetime(dataset["Date"]).dt.strftime('%Y-%V')
然后我根据周数对我的销售额
进行分组,即
data = dataset.groupby(['Date'])["Sales"].sum().reset_index()
data.head(10)
Date Sales
0 2015-01 67797.0
1 2015-02 102714.0
2 2015-03 107011.0
3 2015-04 121480.0
4 2015-05 148098.0
5 2015-06 132152.0
6 2015-07 133914.0
7 2015-08 136160.0
8 2015-09 185471.0
9 2015-10 190793.0
现在我想根据 Date
列创建一个日期范围,因为我要根据周数预测销售额:
ds = data.Date.values
ds_pred = pd.date_range(start=ds.min(), periods=len(ds) + num_pred_weeks,
freq="W")
但是我收到以下错误:无法将字符串转换为时间戳
,我不太确定如何修复。因此,如果我使用 2015-01-01
作为我的日期导入的开始日期,我不会收到任何错误,这让我意识到我使用的函数是错误的。但是,我不确定该怎么做?
我想基本上有一个日期范围,从当前周开始每周跨越,然后是 future 52 周。
最佳答案
我认为问题是要创建最少的 dataset["Date"]
列,这些列由格式为 YYYY-VV
的字符串填充。但是要传递给 date_range
需要格式 YYYY-MM-DD
或 datetime 对象。
我找到了 this :
Several additional directives not required by the C89 standard are included for convenience. These parameters all correspond to ISO 8601 date values. These may not be available on all platforms when used with the strftime() method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling strptime() with incomplete or ambiguous ISO 8601 directives will raise a ValueError.
%V ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4.
YYYY-VV
格式的 Pandas 0.24.2 错误:
dataset = pd.DataFrame({'Date':['2015-06-01','2015-06-02']})
dataset["Date"] = pd.to_datetime(dataset["Date"]).dt.strftime('%Y-%V')
print (dataset)
Date
0 2015-23
1 2015-23
ds = pd.to_datetime(dataset['Date'], format='%Y-%V')
print (ds)
ValueError: 'V' is a bad directive in format '%Y-%V'
可能的解决方案是使用%U
或%W,检查this :
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
dataset = pd.DataFrame({'Date':['2015-06-01','2015-06-02']})
dataset["Date"] = pd.to_datetime(dataset["Date"]).dt.strftime('%Y-%U')
print (dataset)
Date
0 2015-22
1 2015-22
ds = pd.to_datetime(dataset['Date'] + '-1', format='%Y-%U-%w')
print (ds)
0 2015-06-01
1 2015-06-01
Name: Date, dtype: datetime64[ns]
或者在日期时间中使用来自原始 DataFrame 的数据:
dataset = pd.DataFrame({'Date':['2015-06-01','2015-06-02'],
'Sales':[10,20]})
dataset["Date"] = pd.to_datetime(dataset["Date"])
print (dataset)
Date Sales
0 2015-06-01 10
1 2015-06-02 20
data = dataset.groupby(dataset['Date'].dt.strftime('%Y-%V'))["Sales"].sum().reset_index()
print (data)
Date Sales
0 2015-23 30
num_pred_weeks = 5
ds = data.Date.values
ds_pred = pd.date_range(start=dataset["Date"].min(), periods=len(ds) + num_pred_weeks, freq="W")
print (ds_pred)
DatetimeIndex(['2015-06-07', '2015-06-14', '2015-06-21',
'2015-06-28',
'2015-07-05', '2015-07-12'],
dtype='datetime64[ns]', freq='W-SUN')
关于python - Pandas 日期范围为 yyyy-ww 返回 "could not convert string to Timestamp",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56326386/
我需要使用正则表达式验证格式为 dd/mm/yyyy 的日期字符串。 此正则表达式验证 dd/mm/yyyy,但不验证 31/02/4500 等无效日期: ^(0?[1-9]|[12][0-9]|3[
我需要检查给定字符串的日期。我得到的字符串不规则,我不想使用 Dateformat 或类似的东西。我试图避免出现多个异常。我需要 dd.MM.yyyy 和 MM.yyyy 和 yyyy 的正则表达式。
我想使用在这里找到的 jQuery 屏蔽输入插件 http://digitalbush.com/projects/masked-input-plugin/格式化日期。 我正在从数据库中提取数据以在页面
我正在尝试以更有效的方式根据当前日期找出当前财政年度(FY - 3 月至 4 月)。这是我到目前为止写的内容 public static void main(String[] args) {
我已经搜索了该网站,但没有找到接近我需要的答案。基本上我的日期正确显示为“2013-05-09”,但我想要的是“2013 年 3 月 19 日”或使其更好“3 月 19 日, 2013 年”。 请向我
我需要使用 C# 更改日期字符串的格式 来自:“06/16/2010”或“16/06/2010” 至:“2010-06-16” 你能帮我实现这个吗 谢谢 最佳答案 如果您已经将它作为 DateTime
我正在以 MMM-yyyy 格式从 View 中提交日期值,即“Apr-2019”作为字符串。我需要将字符串值转换为 .Net 支持的日期格式,如 dd-MM-yyyy 或 yyyy-MM-dd。这里
我有一个包含以下数据的平面文件 2/12/2016 2/3/2017 12/23/2017 04/23/2017 第一个是 M/dd/yyyy,第二个是 M/d/yyyy,其余日期格式为 MM/dd/
好吧,我已经和这个战斗了足够长的时间。我有一个正则表达式 ^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d) 这将匹配 8/15、08/15
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Extending JavaScript's Date.parse to allow for DD/MM/Y
我有 json 对象列表,其中一个字段是日期。问题是日期在 json 中以不同的方式写入。 大多数看起来像: "publishedDate": "2005-01-28" "publishedDat
这个问题已经有答案了: Generate & parse "Year-Month" values in text from Java (1 个回答) 已关闭 7 年前。 我有一个变量: YearMon
我对日期格式有一个奇怪的问题。就像,我正在使用 jquery 日期选择器,它设置为格式 dd/mm/yy。因此,当我在该字段中填写 01/09/2015 时,它应该意味着日期是 2015 年 9 月
我有以下日期, 24/04/2019 13/05/2019 12/04/2019 我想删除年份并将这些日期转换为以下格式, 24/04 13/05 12/04 这不是仅用于输出我想在该日期执行加法或申
“YYYY”和“yyyy”之间的确切区别是什么。我读过 this link ,它指出 A common mistake is to use YYYY. yyyy specifies the calen
我有一个日期列,其中日期格式为 2010-04-14 在 SQL Server 中。有没有办法将日期格式检索为 14/04/2010 在选择语句中? 最佳答案 试试这个: SELECT CONVERT
我有这个正则表达式:(\d{4})\/(\d{2,4})\/?(\w{2})?这似乎工作正常,但我想避免以下情况,例如:我有一个字符串“2020/2000/WEEEE”并且它匹配它。 它匹配的其他错误
这是用户输入日期的代码:(它必须是我的日期选择器,但表单必须将日期字段作为文本提交 - 不要问) 提交时,我在 javascript 中调用验证逻辑。我附上了当我尝试输入 01/01/2001 作为用
我正在尝试以 YYYY-YY 格式提取会计日期字符串的最后一年 (YY)。例如,这个“1999-00”的最后一年是 2000 年。 当前代码似乎涵盖了除此之外的大多数情况。 import pandas
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Conversion of Date 我从对话框中获取的日期为:5-1-2012 - 作为字符串。 我需要将其转换为
我是一名优秀的程序员,十分优秀!