我在熊猫中创建了一个TimeSeries:
In [346]: from datetime import datetime
In [347]: dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7),
.....: datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)]
In [348]: ts = Series(np.random.randn(6), index=dates)
In [349]: ts
Out[349]:
2011-01-02 0.690002
2011-01-05 1.001543
2011-01-07 -0.503087
2011-01-08 -0.622274
2011-01-10 -0.921169
2011-01-12 -0.726213
Run Code Online (Sandbox Code Playgroud)
我正在关注"Python for Data Analysis"一书中的示例.
在以下段落中,作者检查索引类型:
In [353]: ts.index.dtype
Out[353]: dtype('datetime64[ns]')
Run Code Online (Sandbox Code Playgroud)
当我在控制台中执行完全相同的操作时,我得到:
ts.index.dtype
dtype('<M8[ns]')
Run Code Online (Sandbox Code Playgroud)
什么是两种类型之间的区别'datetime64[ns]'和'<M8[ns]'?
为什么我会得到另一种类型?
我有以下数据帧:
user_id purchase_date
1 2015-01-23 14:05:21
2 2015-02-05 05:07:30
3 2015-02-18 17:08:51
4 2015-03-21 17:07:30
5 2015-03-11 18:32:56
6 2015-03-03 11:02:30
Run Code Online (Sandbox Code Playgroud)
并且purchase_date是一个datetime64[ns]专栏.我需要添加一个新列df[month],其中包含购买日期的第一天:
df['month']
2015-01-01
2015-02-01
2015-02-01
2015-03-01
2015-03-01
2015-03-01
Run Code Online (Sandbox Code Playgroud)
我正在寻找像DATE_FORMAT(purchase_date, "%Y-%m-01") mSQL 一样的东西.我试过以下代码:
df['month']=df['purchase_date'].apply(lambda x : x.replace(day=1))
Run Code Online (Sandbox Code Playgroud)
它以某种方式工作但返回:2015-01-01 14:05:21.
我正在解析一个巨大的ascii文件,其中包含分配给条目的日期.所以,我发现自己使用与numpy.datetime64并行的datetime包来添加数组功能.我知道pandas包可能最适合用于约会,但是尝试在没有熊猫的情况下将其拉出来.我一直在寻找一种巧妙的方法来添加/减去某个日期步骤,例如一年或者3个月的datetime64对象.
目前,我正在将dt64对象转换为dt对象,并使用replace函数来更改年份,例如,之后必须将其转换回dt64,这对我来说有点麻烦.所以,如果有人只使用numpy.datetime64格式有更好的解决方案,我将不胜感激.
示例:将"YYYY-12-31"转换为"(YYYY-1)-12-31"
a = np.datetime64(2014,12,31) # a is dt64 object
b = a.astype(object) # b is dt object converted from a
c = np.datetime64( b.replace(b.year-1)) # c is dt64 object shifted back 1 year (a -1year)
Run Code Online (Sandbox Code Playgroud) 我希望按指定的时区看到numpy datetime64对象.
>>> import numpy as np
>>> np.datetime64('2013-03-10T01:30:54')
numpy.datetime64('2013-03-10T01:30:54+0400')
>>> np.datetime64('2013-03-10T01:30:54+0300')
numpy.datetime64('2013-03-10T02:30:54+0400')
Run Code Online (Sandbox Code Playgroud)
Python打印日期时间对象总是在UTC + 0400(它是我的本地时区),即使我指定另一个时区>>> np.datetime64('2013-03-10T01:30:54+0300').有没有办法按UTC + 0000时区强制python打印?
我正在使用numpy 1.8.1.
我认为这应该很容易,但我有点像墙了.我有一个数据集从Stata .dta文件导入到pandas数据框中.其中一些列包含日期数据.数据框包含100,000多行,但给出了一个示例:
cat event_date total
0 G2 2006-03-08 16
1 G2 NaT NaN
2 G2 NaT NaN
3 G3 2006-03-10 16
4 G3 2006-08-04 12
5 G3 2006-12-28 13
6 G3 2007-05-25 10
7 G4 2006-03-10 13
8 G4 2006-08-06 19
9 G4 2006-12-30 16
Run Code Online (Sandbox Code Playgroud)
数据存储为datetime64格式:
>>> mydata[['cat','event_date','total']].dtypes
cat object
event_date datetime64[ns]
total float64
dtype: object
Run Code Online (Sandbox Code Playgroud)
我想要做的就是创建一个新列,它在event_date和开始日期之间(例如2006-01-01)给出了天数(而不是'us'或'ns'!!!)的差异.我尝试过以下方法:
>>> mydata['new'] = mydata['event_date'] - np.datetime64('2006-01-01')
Run Code Online (Sandbox Code Playgroud)
......但我收到的消息是:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely …Run Code Online (Sandbox Code Playgroud) 访问时DataFrame.values,所有pd.Timestamp对象都转换为np.datetime64对象,为什么?包含对象可以存在,因此我不明白为什么总是会发生这种自动转换np.ndarray。pd.Timestamp
你知道如何预防吗?
最小的例子:
import numpy as np
import pandas as pd
from datetime import datetime
# Let's declare an array with a datetime.datetime object
values = [datetime.now()]
print(type(values[0]))
> <class 'datetime.datetime'>
# Clearly, the datetime.datetime objects became pd.Timestamp once moved to a pd.DataFrame
df = pd.DataFrame(values, columns=['A'])
print(type(df.iloc[0][0]))
> <class 'pandas._libs.tslibs.timestamps.Timestamp'>
# Just to be sure, lets iterate over each datetime and manually convert them to pd.Timestamp
df['A'].apply(lambda x: pd.Timestamp(x)) …Run Code Online (Sandbox Code Playgroud) 我似乎无法使用numpy.digitize来处理datetime64:
date_bins = np.array([np.datetime64(datetime.datetime(2014, n, 1), 's') for n in range(1,13)])
np.digitize(date_bins, date_bins)
Run Code Online (Sandbox Code Playgroud)
它给出以下错误:
TypeError: Cannot cast array data from dtype('<M8[s]') to dtype('float64') according to the rule 'safe'
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?
我有一个处理 Excel 文件的脚本。发送它的部门有一个生成它的系统,我的脚本停止工作。
我突然收到Can only use .str accessor with string values, which use np.object_ dtype in pandas以下代码行的错误:
df['DATE'] = df['Date'].str.replace(r'[^a-zA-Z0-9\._/-]', '')
Run Code Online (Sandbox Code Playgroud)
我检查了旧系统文件中日期列的类型(dtype:object)与来自新系统的文件(dtype:datetime64[ns])。
如何将日期格式更改为我的脚本可以理解的格式?
我看到了这个答案,但我对日期格式的了解并不是那么精细。
有没有一种方法可以计算并以datetime格式返回datetime列的中位数?我想计算datetime64 [ns]格式的python列的中位数。以下是该列的示例:
df['date'].head()
0 2017-05-08 13:25:13.342
1 2017-05-08 16:37:45.545
2 2017-01-12 11:08:04.021
3 2016-12-01 09:06:29.912
4 2016-06-08 03:16:40.422
Run Code Online (Sandbox Code Playgroud)
名称:recency,dtype:datetime64 [ns]
我的目标是使中位数与上述日期列的日期时间格式相同:
尝试转换为np.array:
median_ = np.median(np.array(df['date']))
Run Code Online (Sandbox Code Playgroud)
但这引发了错误:
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')
Run Code Online (Sandbox Code Playgroud)
转换为int64然后计算中位数并尝试将格式返回给datetime无效
df['date'].astype('int64').median().astype('datetime64[ns]')
Run Code Online (Sandbox Code Playgroud) 如何从数据帧中删除两个时间戳之间的所有行?
我的数据框看起来像:
b a
0 2016-12-02 22:00:00 19.218519
1 2016-12-02 23:00:00 19.171197
2 2016-12-03 00:00:00 19.257836
3 2016-12-03 01:00:00 19.195610
4 2016-12-03 02:00:00 19.176413
Run Code Online (Sandbox Code Playgroud)
例如:我想删除上面数据帧中时间戳介于“2016-12-02 22:00:00”到“2016-12-03 00:00:00”之间的所有行。因此,结果将仅包含第 3 行和第 4 行。
b列的类型是datetime64,a列的类型是float。
请建议。
datetime64 ×10
python ×9
pandas ×7
numpy ×4
dataframe ×3
datetime ×3
date ×1
python-2.7 ×1
python-3.x ×1
timestamp ×1
timezone ×1