找到关于编程的34423 篇文章

Python Pandas - 将 DatetimeIndex 作为 datetime.datetime 对象的 object ndarray 返回

AmitDiwan
更新于 2021年10月19日 10:06:45

392 次浏览

要将 DatetimeIndex 作为 datetime.datetime 对象的 object ndarray 返回,请使用 datetimeindex.to_pydatetime() 方法。首先,导入所需的库 - import pandas as pd 创建一个周期为 5、频率为 Y(即年份)的 DatetimeIndex - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') 显示 DateTimeIndex - print("DateTimeIndex...", datetimeindex) 返回 DatetimeIndex 作为对象 ndarray - print("Return DatetimeIndex as object ndarray of datetime.datetime objects...", datetimeindex.to_pydatetime()) 示例 以下代码 - import pandas as pd # 周期为 5,频率为 Y(即年份)的 DatetimeIndex datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') # 显示 DateTimeIndex print("DateTimeIndex...", datetimeindex) # 显示 DateTimeIndex 频率 print("DateTimeIndex frequency...", datetimeindex.freq) # 将 DateTimeIndex 作为 ... 阅读更多

Python Pandas - 计算索引值与以指定频率转换为 PeriodArray 的索引之间的 TimedeltaArray 差值

AmitDiwan
更新于 2021年10月19日 10:05:15

186 次浏览

要计算索引值与以指定频率转换为 PeriodArray 的索引之间的 TimedeltaArray 差值,请使用 datetimeindex.to_perioddelta() 方法。使用 freq 参数设置频率。首先,导入所需的库 - import pandas as pd 创建一个周期为 7,频率为 Y(即年份)的 DatetimeIndex - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') 显示 DateTimeIndex - print("DateTimeIndex...", datetimeindex) 计算索引值与转换为 PeriodArray 的索引之间的 TimedeltaArray 差值。我们使用值为“M”的“freq”参数设置了 Period 频率 - print("Convert DateTimeIndex to PeriodDelta...", datetimeindex.to_perioddelta(freq='M')) 示例 以下代码 - import pandas as pd # 周期为 7 的 DatetimeIndex ... 阅读更多

Python Pandas - 如何将 DateTimeIndex 转换为 Period

AmitDiwan
更新于 2021年10月19日 10:04:05

2K+ 次浏览

要将 DateTimeIndex 转换为 Period,请在 Pandas 中使用 datetimeindex.to_period() 方法。频率使用 freq 参数设置。首先,导入所需的库 - import pandas as pd 创建一个周期为 5,频率为 Y(即年份)的 DatetimeIndex - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') 显示 DateTimeIndex - print("DateTimeIndex...", datetimeindex) 将 DateTimeIndex 转换为 Period。我们使用值为“M”的“freq”参数将频率设置为月份 - print("Convert DateTimeIndex to Period...", datetimeindex.to_period(freq='M')) 示例 以下代码 - import pandas as pd # 周期为 5,频率为 Y(即年份)的 DatetimeIndex # 时区为 Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') ... 阅读更多

Python 中通过列重排查找最大子矩阵面积的程序

Arnab Chakraborty
更新于 2021年10月19日 10:07:15

254 次浏览

假设我们有一个二进制矩阵。我们可以根据需要多次重新排列列,然后找到并返回仅包含 1 的最大子矩阵的面积。因此,如果输入类似于 100111101,则输出将为 4,因为我们可以将其排列为 - 100111110 要解决这个问题,我们将遵循以下步骤 - n := 矩阵的行数 m := 矩阵的列数 ans := 0 for i in range 1 to n - 1, do for j in range 0 to m - 1, do if matrix[i, j] is 1, then matrix[i, j] := matrix[i, j] + matrix[i-1, j] 对于每一行 ... 阅读更多

Python Pandas - 如何对具有指定频率的 DateTimeIndex 执行向上取整运算

AmitDiwan
更新于 2021年10月19日 10:02:32

84 次浏览

要对具有指定频率的 DateTimeIndex 执行向上取整运算,请使用 DateTimeIndex.ceil() 方法。对于频率,请使用 freq 参数。首先,导入所需的库 - import pandas as pd 创建一个周期为 5,频率为 S(即秒)的 DatetimeIndex。时区为 Australia/Adelaide - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') 显示 DateTimeIndex - print("DateTimeIndex...", datetimeindex) 对 DateTimeIndex 日期进行向上取整运算,并指定频率 - print("Performing ceil operation...", datetimeindex.ceil(freq='us')) 示例 以下代码 - import pandas as pd # 周期为 5,频率为 S(即秒)的 DatetimeIndex # 时区为 Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') # 显示 DateTimeIndex ... 阅读更多

Python 中查找列表中第 k 小元素的程序

Arnab Chakraborty
更新于 2021年10月19日 10:03:28

165 次浏览

假设我们有三个值 n、total 和 k。现在考虑一个大小为 n 的列表,其总和与 total 相同,并且任何两个连续元素之间的绝对差最多为 1。我们必须找到此列表中索引 k 处的最大值。因此,如果输入类似于 n = 5 total = 15 k = 3,则输出将为 4,因为一个可能的列表如下所示:[3, 2, 3, 4, 3],在索引 3 处找到的最大元素为 4。要解决这个问题,我们将遵循以下步骤 - x := ... 阅读更多

Python Pandas - 如何对具有微秒频率的 DateTimeIndex 执行向上取整运算

AmitDiwan
更新于 2021年10月19日 10:01:04

94 次浏览

要对具有微秒频率的 DateTimeIndex 执行向上取整运算,请使用 DateTimeIndex.ceil() 方法。对于微秒频率,请使用值为“us”的 freq 参数。首先,导入所需的库 - import pandas as pd 创建一个周期为 5,频率为 S(即秒)的 DatetimeIndex - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') 显示 DateTimeIndex - print("DateTimeIndex...", datetimeindex) 对 DateTimeIndex 日期进行向上取整运算,并使用微秒频率。对于微秒频率,我们使用了“us” - print("Performing ceil operation with microseconds frequency...", datetimeindex.ceil(freq='us')) 示例 以下代码 - import pandas as pd # 周期为 5,频率为 S(即秒)的 DatetimeIndex # 时区为 Australia/Adelaide ... 阅读更多

Python Pandas - 如何对具有毫秒频率的 DateTimeIndex 执行向上取整运算

AmitDiwan
更新于 2021年10月19日 09:59:56

130 次浏览

要对具有毫秒频率的 DateTimeIndex 执行向上取整运算,请使用 DateTimeIndex.ceil() 方法。对于毫秒频率,请使用值为“ms”的 freq 参数。首先,导入所需的库 - import pandas as pd 创建一个周期为 5,频率为 S(即秒)的 DatetimeIndex。时区为 Australia/Adelaide - datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') 对 DateTimeIndex 日期进行向上取整运算,并使用毫秒频率。对于毫秒频率,我们使用了“ms” - print("Performing ceil operation with milliseconds frequency...", datetimeindex.ceil(freq='ms')) 示例 以下代码 - import pandas as pd # 周期为 5,频率为 S(即秒)的 DatetimeIndex # 时区为 Australia/Adelaide datetimeindex ... 阅读更多

Python 中线性时间内查找第 k 小元素的程序

Arnab Chakraborty
更新于 2021年10月19日 10:01:16

2K+ 次浏览

假设我们有一个名为nums的数字列表,我们还有一个值k,我们必须找到列表中第k个(从0开始)最小的元素。我们必须平均在O(n)时间内解决这个问题。因此,如果输入类似于nums = [6, 4, 9, 3, 1] k = 2,则输出将为4,因为排序后的列表将类似于[1, 3, 4, 6, 9],第k个最小元素为4。为了解决这个问题,我们将遵循以下步骤:maxHeap := 一个新的空堆;对于范围0到k内的i,执行插入……阅读更多

Python Pandas - 如何对具有秒频率的DateTimeIndex执行上舍入运算

AmitDiwan
更新于 2021年10月19日 09:58:31

98 次浏览

要对具有秒频率的DateTimeIndex执行上舍入运算,请使用DateTimeIndex.ceil()方法。对于秒频率,请使用值为“S”的freq参数。首先,导入所需的库:import pandas as pd 创建一个周期为5且频率为S(即秒)的DatetimeIndex:datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') 显示DateTimeIndex:print("DateTimeIndex...", datetimeindex) 对DateTimeIndex日期执行具有秒频率的上舍入运算。对于秒频率,我们使用了'S':print("Performing ceil operation with seconds frequency...", datetimeindex.ceil(freq='S')) 示例 以下是代码:import pandas as pd # 周期为5且频率为S(即秒)的DatetimeIndex # 时区为Australia/Adelaide ...阅读更多

广告
© . All rights reserved.