我有这段代码:
date_counter = Time.mktime(2011,01,01,00,00,00,"+05:00")
@weeks = Array.new
(date_counter..Time.now).step(1.week) do |week|
logger.debug "WEEK: " + week.inspect
@weeks << week
end
Run Code Online (Sandbox Code Playgroud)
从技术上讲,代码有效,输出:
Sat Jan 01 00:00:00 -0500 2011
Sat Jan 08 00:00:00 -0500 2011
Sat Jan 15 00:00:00 -0500 2011
etc.
Run Code Online (Sandbox Code Playgroud)
但执行时间完全是垃圾!每周计算大约需要4秒钟.
我在这段代码中遗漏了一些奇怪的低效率吗?看起来很简单.
我正在使用Rails 3.0.3运行Ruby 1.8.7.
我有一个带有"日期"字段的访问表.它有每个记录的随机日期.我已经构建了一个脚本来将所有记录附加到列表中,然后设置列表以仅过滤掉唯一值:
dateList = []
# cursor search through each record and append all records in the date
# field to a python list
for row in rows:
dateList.append(row.getValue("DATE_OBSERVATION").strftime('%m-%d-%Y'))
# Filter unique values to a set
newList = list(set(dateList))
Run Code Online (Sandbox Code Playgroud)
这将返回(在我的测试表上):
['07 -06-2010','06 -24-2010','07 -05-2010','06 -25-2010']
既然我有"DATE_OBSERVATION"字段的唯一值,我想检测是否:
我们欢迎所有的建议!麦克风
我在PostgreSQL(9.3)中有一个带有daterange字段类型的表.
我可以像使用JDBC的String一样选择此字段,但我无法将其插入表中.
我尝试过的:
PreparedStatement stm = conn.prepareStatement("insert into mytable (my_daterange_field) values (?)");
stm.setString(1, "[2014-01-02,2014-01-04]");
int i = stm.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
我得到了:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "my_daterange_field" is of type daterange but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 168
Run Code Online (Sandbox Code Playgroud)
有没有人有插入日期范围的解决方案?我应该使用什么stm.setXXX?或许我不能这样做,因为JDBC Driver没有日期范围的支持......也许有第三种解决方案?
谢谢.
PS
我的PostgreSQL JDBC驱动程序:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种有效的方法来查找时间戳范围集之间的所有交叉点.它需要与PostgreSQL 9.2一起使用.
假设范围代表一个人可以见面的时间.每个人在可用时可能有一个或多个时间范围.我想找到一个会议可以进行的所有时间段(即,在此期间所有人都可用).
这是我到目前为止所得到的.它似乎有效,但我不认为它非常有效,因为它一次只考虑一个人的可用性.
WITH RECURSIVE td AS
(
-- Test data. Returns:
-- ["2014-01-20 00:00:00","2014-01-31 00:00:00")
-- ["2014-02-01 00:00:00","2014-02-20 00:00:00")
-- ["2014-04-15 00:00:00","2014-04-20 00:00:00")
SELECT 1 AS entity_id, '2014-01-01'::timestamp AS begin_time, '2014-01-31'::timestamp AS end_time
UNION SELECT 1, '2014-02-01', '2014-02-28'
UNION SELECT 1, '2014-04-01', '2014-04-30'
UNION SELECT 2, '2014-01-15', '2014-02-20'
UNION SELECT 2, '2014-04-15', '2014-05-05'
UNION SELECT 3, '2014-01-20', '2014-04-20'
)
, ranges AS
(
-- Convert to tsrange type
SELECT entity_id, tsrange(begin_time, end_time) AS the_range
FROM td …Run Code Online (Sandbox Code Playgroud) 我的数据库中有一个日期字段,其日期类型为数据类型,如何根据特定日期范围使用活动记录来获取数据?
$model = ModelName::find()
->where(["date"=>"FROM '2015-06-21' TO '2015-06-27' ", "status"=>1])->all();
Run Code Online (Sandbox Code Playgroud) 我需要为表中每个人的 2 个日期之间的每个月创建一行(结果应该是当月的第一天)。例如,如果我的源表中有以下数据:
rowID | person | startdate | enddate
1 | 12345 | 2014-04-01 | 2014-11-30
2 | 67890 | 2014-03-01 | 2014-05-01
Run Code Online (Sandbox Code Playgroud)
我希望我的目标表中的结果是:
person | month
12345 | 2014-04-01
12345 | 2014-05-01
12345 | 2014-06-01
12345 | 2014-07-01
12345 | 2014-08-01
12345 | 2014-09-01
12345 | 2014-10-01
12345 | 2014-11-01
67890 | 2014-03-01
67890 | 2014-04-01
67890 | 2014-05-01
Run Code Online (Sandbox Code Playgroud)
非常感谢帮忙。
有些数据每 5 秒收集一次,有时会丢失。
将它们加载到 Pandas 数据帧后,我想定义一个时间起点并准确提取 180 行(15 分钟 x 每分钟 12 个样本),无论起点如何。这些数据提供了一个绘图,并且始终保持相同的大小可以大大简化其余代码。
缺失的数据应该用 None 填充。
我认为有一些我不知道的快捷方式可以做到这一点:
import pandas as pd
import datetime
dt = [
"2018-02-08 13:45:05",
"2018-02-08 13:45:10",
"2018-02-08 13:45:25",
"2018-02-08 13:45:30",
"2018-02-08 13:45:35",
"2018-02-08 13:45:40",
"2018-02-08 13:45:50",
"2018-02-08 13:45:55",
"2018-02-08 13:46:00",
"2018-02-08 13:46:05",
]
wl = [
4737.25,
4834.80,
4885.53,
5003.98,
5031.08,
5215.90,
5147.65,
5100.50,
5038.94,
5020.67,
]
df = pd.DataFrame({"dt":dt, "wl":wl}).set_index("dt")
df.index = pd.to_datetime(df.index)
df = df.resample("5s").mean()
print(df)
Run Code Online (Sandbox Code Playgroud)
返回:
wl
dt
2018-02-08 …Run Code Online (Sandbox Code Playgroud) 关于在日期或时间范围内找到重叠有一些问题(例如).我用这些来解决我的问题,但我最终得到了一个非常缓慢(而且不是很优雅)的解决方案来解决我的问题.如果有人知道如何更快(更优雅),我会非常感激:
问题:
我有2个dataframes,df1并且df2,每2列代表的开始时间和结束时间:
>>> df1
datetime_start datetime_end
0 2016-09-11 06:00:00 2016-09-11 06:30:00
1 2016-09-11 07:00:00 2016-09-11 07:30:00
2 2016-09-11 07:30:00 2016-09-11 08:00:00
3 2016-09-11 08:00:00 2016-09-11 08:30:00
4 2016-09-11 08:30:00 2016-09-11 09:00:00
5 2016-09-11 09:00:00 2016-09-11 09:30:00
6 2016-09-11 09:30:00 2016-09-11 10:00:00
7 2016-09-11 10:30:00 2016-09-11 11:00:00
13 2016-09-11 14:00:00 2016-09-11 14:30:00
14 2016-09-11 14:30:00 2016-09-11 15:00:00
15 2016-09-11 15:00:00 2016-09-11 15:30:00
16 2016-09-11 15:30:00 2016-09-11 16:00:00
17 2016-09-11 16:00:00 …Run Code Online (Sandbox Code Playgroud) 我有一个与此类似的问题。
然而,我正在处理一个巨大的数据集。我想看看我是否可以在 PySpark 而不是 pandas 中做同样的事情。下面是 pandas 中的解决方案。这可以在 PySpark 中完成吗?
def merge_dates(grp):
# Find contiguous date groups, and get the first/last start/end date for each group.
dt_groups = (grp['StartDate'] != grp['EndDate'].shift()).cumsum()
return grp.groupby(dt_groups).agg({'StartDate': 'first', 'EndDate': 'last'})
# Perform a groupby and apply the merge_dates function, followed by formatting.
df = df.groupby(['FruitID', 'FruitType']).apply(merge_dates)
df = df.reset_index().drop('level_2', axis=1)
Run Code Online (Sandbox Code Playgroud) 我在扩展中有多个表单,我正在使用此代码作为日期选择器,
<mat-form-field>
<input formControlName="date" matInput placeholder="Dátum" type="date">
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
哪个工作得很好,扩展并没有隐藏它(看看),但是我有日期和时间的开始和结束值,所以我切换到ngx material daterangepicker。
我唯一的问题是扩展隐藏了 daterangepicker (have a look),所以 daterangepickers 代码如下所示:
<mat-form-field>
<input
matInput
ngxDaterangepickerMd
name="daterange"
placeholder="Choose date"
applyLabel="Okay"
startKey="start"
endKey="end"
firstMonthDayClass="first-day"
lastMonthDayClass="last-day"
emptyWeekRowClass="empty-week"
lastDayOfPreviousMonthClass="last-previous-day"
firstDayOfNextMonthClass="first-next-day"
[autoApply]="options.autoApply"
[linkedCalendars]="options.linkedCalendars"
[singleDatePicker]="options.singleDatePicker"
[showDropdowns]="true"
formControlName="date"
[showWeekNumbers]="options.showWeekNumbers"
[showCancel]="options.showCancel"
[showClearButton]="options.showClearButton"
[showISOWeekNumbers]="options.showISOWeekNumbers"
[customRangeDirection]="options.customRangeDirection"
[lockStartDate]="options.lockStartDate"
[closeOnAutoApply]="options.closeOnAutoApply"
[opens]="opens"
[drops]="drops"
[timePicker]="timePicker"
/>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
我试图给它一个自定义的 z-index,比如:
.md-drppicker {
z-index: 9999;
}
ngx-daterangepicker-material {
z-index: 9999;
}
Run Code Online (Sandbox Code Playgroud)
但这并没有解决问题,也尝试弄乱显示/位置,但我无法修复它。
知道出了什么问题吗?
date-range ×10
postgresql ×3
python ×3
pandas ×2
angular ×1
apache-spark ×1
css ×1
database ×1
date ×1
datetime ×1
java ×1
jdbc ×1
ngx-daterangepicker-material ×1
pyspark ×1
range ×1
ruby ×1
sql ×1
yii2 ×1
z-index ×1