小编Ped*_*raz的帖子

在Python中获取日期列表中每个月的最后一个日期

我正在使用 Python 2.7、PyCharm 和 Anaconda,

我有一个list日期,我想检索数组中存在的每个月的最后一个日期。

有没有任何函数或库可以帮助我做到这一点?

我从 CSV 文件中读取日期并将其存储为datetime.

我有以下代码:

Dates=[]
Dates1=[]
for date in dates:
    temp=xlrd.xldate_as_tuple(int(date),0)
    Dates1.append(datetime.datetime(temp[0],temp[1],temp[2]))

for date in Dates1:
    if not (date<startDate or date>endDate):
        Dates.append(date)
Run Code Online (Sandbox Code Playgroud)

为了说清楚,假设我有:

Dates = [2015-01-20, 2015-01-15, 2015-01-17, 2015-02-21, 2015-02-06] 
Run Code Online (Sandbox Code Playgroud)

(考虑它是datetime格式化的。)

我想要检索的列表是:

[2015-01-20, 2015-02-21]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经用谷歌搜索过,尤其是在 Stack Overflow 中,但我只能找到如何获取每个月最后日期的答案,而不能从用户指定的列表中找到答案。

python numpy date

3
推荐指数
1
解决办法
6533
查看次数

NumPy array_equal 函数没有按预期工作

array_equalNumPy. 我确定我遗漏了一个非常简单的细节。

我想要完成的是单元测试。

事实是,我有数组 1:

In[21]: bounds['lowerBound']

Out[21]: array([ 1. ,  1.2,  1.8])
Run Code Online (Sandbox Code Playgroud)

我有数组 2:

In[22]: res['lowerBound']

Out[22]: array([ 1. ,  1.2,  1.8])
Run Code Online (Sandbox Code Playgroud)

以防万一我确实检查了它们的形状:

In[26]: bounds['lowerBound'].shape
Out[26]: (3,)
In[28]: res['lowerBound'].shape
Out[28]: (3,)
Run Code Online (Sandbox Code Playgroud)

还有dtypes:

In[30]: res['lowerBound'].dtype
Out[30]: dtype('float64')
In[31]: bounds['lowerBound'].dtype
Out[31]: dtype('float64')
Run Code Online (Sandbox Code Playgroud)

仍然当我尝试验证它们是否相同时:

In[29]:  np.array_equal(bounds['lowerBound'],res['lowerBound'])
Out[29]: False
Run Code Online (Sandbox Code Playgroud)

怎么可能 ?

提前致谢 !

编辑:用于生成数据的代码是:

bounds={'lowerBound':np.array([1.,1.2,1.8]), 'upperBound':np.array([10.,12.,18.])}
Run Code Online (Sandbox Code Playgroud)

res字典由以下函数生成:

def generateAdjustedBounds(self,universeMktCap,lowerBound,upperBound):
    lb=np.zeros(len(universeMktCap))
    ub=np.zeros(len(universeMktCap))
    lb[0]=lowerBound
    ub[0]=upperBound

    for dat in range(1,len(lb)):
        lb[dat]=lb[dat-1]*universeMktCap[dat]/universeMktCap[dat-1]
        ub[dat]=ub[dat-1]*universeMktCap[dat]/universeMktCap[dat-1]

    Bounds={'lowerBound':np.array(lb),'upperBound':np.array(ub)}

    return Bounds
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

3
推荐指数
1
解决办法
2118
查看次数

是否可以在MatLab中的脚本中定义本地函数?

我做了一些查找,找到了一些答案,但没有一个明确或确定.

我只是想知道,是否可以MatLab在scripts(m-file)中本地定义函数?

因为有时我只想要一个我不会用于任何其他脚本的小功能,所以我不想为它创建一个新文件.为了便于处理它们.

matlab function

2
推荐指数
1
解决办法
261
查看次数

使用 tkinter 将多个框架与小部件组合在一个窗口中

我正在尝试构建一个基本上只有一个窗口的 GUI。该窗口是不同框架的组合。我想要做的是将这些框架(每个框架进一步包含多个小部件)以某种格式排列在一起:

  • 第一帧(左上):窗口大小的一半(从上到左)
  • 第二帧(左下):从底部到左侧的窗口大小的一半
  • 第三帧(右上):窗口顶部尺寸的 1/3,窗口右侧尺寸的一半。
  • 第四帧(右下):窗户上剩余的空间。

下面,我为该问题添加了一个简单的简约代码。为了简单起见,我使用了相同的 FRAME 类并创建了该类的 4 个实例以添加到主窗口中。我使用 Tkinter 的网格管理器来组织框架中的小部件,然后再次使用网格管理器将这些框架打包到主窗口中。

import Tkinter as tk 

# Test frame class. The same frame is used to create 4 instances. 
class TestFrame(tk.Frame): 
    def __init__(self, master=None, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        super().__init__(master)
        
        # Test Entry fields
        test_label_1                = tk.Label(master, text="Test Label 1").grid(row=0, column=0)
        test_label_1_entry          = tk.Entry(master, bd=5).grid(row=0, column=1)
        test_label_2                = tk.Label(master, text="Test Label 2").grid(row=0, column=2)
        test_label_2_entry          = tk.Entry(master, bd=5).grid(row=0, column=3)
    
        # Test checkbox fields
        test_checkbox_1_label       = tk.Label(master, text="Test check button …
Run Code Online (Sandbox Code Playgroud)

python user-interface tkinter

2
推荐指数
1
解决办法
6187
查看次数

numpy multi_dot如何比numpy.dot慢?

我正在尝试优化一些执行大量顺序矩阵运算的代码.

我想numpy.linalg.multi_dot(这里的文档)将执行C或BLAS中的所有操作,因此它会比执行类似arr1.dot(arr2).dot(arr3)等等更快.

我真的很惊讶在笔记本上运行这段代码:

v1 = np.random.rand(2,2)

v2 = np.random.rand(2,2)



%%timeit 
    ?    
v1.dot(v2.dot(v1.dot(v2)))

The slowest run took 9.01 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.14 µs per loop



%%timeit        ?

np.linalg.multi_dot([v1,v2,v1,v2])

The slowest run took 4.67 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 32.9 µs per loop
Run Code Online (Sandbox Code Playgroud)

要发现相同的操作使用速度慢了10倍 …

python performance numpy cython

0
推荐指数
1
解决办法
880
查看次数