StringTokenizer分隔一次(StringTokenizer delimit once)
我想分割一个字符串(行)的第一个空白,但只有第一个。
StringTokenizer linesplit = new StringTokenizer(line," ");
以“这是一个测试”为例。 然后我想要这些字符串是“This”,“是一个测试”。 我怎么能使用StringTokenizer或有更好的选择?
I want to split a string (line) by the first whitespace, but only the first.
StringTokenizer linesplit = new StringTokenizer(line," ");
Take for example "This is a test". Then I want the strings to be "This" and "is a test". How could I use StringTokenizer or is there a better option?
原文:https://stackoverflow.com/questions/22015892
最满意答案
这是在0.12中引入的。 它在cython中完成的速度要快得多。 单位是纪元秒数(如果你的日期时间是以纪元为单位的毫秒数,你也可以传递'ms')。 这里的文档
In [6]: df['end_time'] = pd.to_datetime(df['end_time'],unit='s') In [7]: df['start_time'] = pd.to_datetime(df['end_time'],unit='s') In [8]: df Out[8]: end_time start_time 0 2013-10-03 13:04:41 2013-10-03 13:04:41 1 2013-10-03 13:04:42 2013-10-03 13:04:42 2 2013-10-03 13:04:43 2013-10-03 13:04:43 3 2013-10-03 13:04:44 2013-10-03 13:04:44 4 2013-10-03 13:04:45 2013-10-03 13:04:45 5 2013-10-03 13:04:46 2013-10-03 13:04:46 6 2013-10-03 13:04:47 2013-10-03 13:04:47 7 2013-10-03 13:04:48 2013-10-03 13:04:48 8 2013-10-03 13:04:49 2013-10-03 13:04:49 9 2013-10-03 13:04:50 2013-10-03 13:04:50
请注意,这已经是GMT。
datetime.fromtimestamp
转换为本地tz)。 如果你想要那个。In [21]: DatetimeIndex(pd.to_datetime(df['end_time'],unit='s'),tz='UTC').tz_convert('EST') Out[21]: <class 'pandas.tseries.index.DatetimeIndex'> [2013-10-03 08:04:41, ..., 2013-10-03 08:04:50] Length: 10, Freq: None, Timezone: EST In [32]: DataFrame(dict(end_time = DatetimeIndex(pd.to_datetime(df['end_time'],unit='s'),tz='UTC').tz_convert('Asia/Kolkata').asobject))
要转换为亚洲/加尔各答的tz。 你必须将其表示为对象。 这将是有效的。
end_time 0 2013-10-03 18:34:41+05:30 1 2013-10-03 18:34:42+05:30 2 2013-10-03 18:34:43+05:30 3 2013-10-03 18:34:44+05:30 4 2013-10-03 18:34:45+05:30 5 2013-10-03 18:34:46+05:30 6 2013-10-03 18:34:47+05:30 7 2013-10-03 18:34:48+05:30 8 2013-10-03 18:34:49+05:30 9 2013-10-03 18:34:50+05:30
This was introduced in 0.12. MUCH faster as its all done in cython. the unit is number of epoch seconds (you can also pass for example 'ms' if your datetimes in milliseconds since epoch). docs here
In [6]: df['end_time'] = pd.to_datetime(df['end_time'],unit='s') In [7]: df['start_time'] = pd.to_datetime(df['end_time'],unit='s') In [8]: df Out[8]: end_time start_time 0 2013-10-03 13:04:41 2013-10-03 13:04:41 1 2013-10-03 13:04:42 2013-10-03 13:04:42 2 2013-10-03 13:04:43 2013-10-03 13:04:43 3 2013-10-03 13:04:44 2013-10-03 13:04:44 4 2013-10-03 13:04:45 2013-10-03 13:04:45 5 2013-10-03 13:04:46 2013-10-03 13:04:46 6 2013-10-03 13:04:47 2013-10-03 13:04:47 7 2013-10-03 13:04:48 2013-10-03 13:04:48 8 2013-10-03 13:04:49 2013-10-03 13:04:49 9 2013-10-03 13:04:50 2013-10-03 13:04:50
Note that this is in GMT already.
datetime.fromtimestamp
does a conversion to local tz). If you want that.In [21]: DatetimeIndex(pd.to_datetime(df['end_time'],unit='s'),tz='UTC').tz_convert('EST') Out[21]: <class 'pandas.tseries.index.DatetimeIndex'> [2013-10-03 08:04:41, ..., 2013-10-03 08:04:50] Length: 10, Freq: None, Timezone: EST In [32]: DataFrame(dict(end_time = DatetimeIndex(pd.to_datetime(df['end_time'],unit='s'),tz='UTC').tz_convert('Asia/Kolkata').asobject))
To convert to the tz of Asia/Kolkata. You have to represent this as object. This is will work.
end_time 0 2013-10-03 18:34:41+05:30 1 2013-10-03 18:34:42+05:30 2 2013-10-03 18:34:43+05:30 3 2013-10-03 18:34:44+05:30 4 2013-10-03 18:34:45+05:30 5 2013-10-03 18:34:46+05:30 6 2013-10-03 18:34:47+05:30 7 2013-10-03 18:34:48+05:30 8 2013-10-03 18:34:49+05:30 9 2013-10-03 18:34:50+05:30
相关问答
更多-
由于DatetimeIndex是ndarray ,所以你可以在没有理解的情况下进行转换(更快)。 In [1]: import numpy as np In [2]: import pandas as pd In [3]: from datetime import datetime In [4]: dates = [datetime(2012, 5, 1), datetime(2012, 5, 2), datetime(2012, 5, 3)] ...: index = pd.DatetimeI ...
-
您可以使索引包含具有不同时区的Timestamps 。 但你必须明确地将其构建为Index 。 In [33]: pd.Index([pd.Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London'),pd.Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels')],dtype='object') Out[33]: Index([2015-06-07 23:00:00+01:00, 2015- ...
-
您需要访问单行Series的标量值并将其传递给loc以对df进行切片: data.loc[start.index[0] : end.index[0]] You needed to access the scalar value inside the single row Series and pass this to loc to slice the df: data.loc[start.index[0] : end.index[0]]
-
根据时间间隔使用DatetimeIndex切片Pandas数据帧(Slice a Pandas dataframe with DatetimeIndex based on time interval)[2022-04-14]
看来你需要通过索引[0]创建标量并按loc选择: df = bro_df.loc[first_entry[0]: last_entry[0]] 或者通过精确索引选择: df = bro_df[first_entry[0]: last_entry[0]] 样品 : rng = pd.date_range('2017-04-03', periods=10, freq='2H 24T') bro_df = pd.DataFrame({'a': range(10)}, index=rng) print (b ... -
这是在0.12中引入的。 它在cython中完成的速度要快得多。 单位是纪元秒数(如果你的日期时间是以纪元为单位的毫秒数,你也可以传递'ms')。 这里的文档 In [6]: df['end_time'] = pd.to_datetime(df['end_time'],unit='s') In [7]: df['start_time'] = pd.to_datetime(df['end_time'],unit='s') In [8]: df Out[8]: end_time ...
-
Pandas date_range从DatetimeIndex到Date格式(Pandas date_range from DatetimeIndex to Date format)[2022-03-24]
to_pydatetime返回Python datetime.datetime对象的NumPy数组: In [8]: dates = rng.to_pydatetime() In [9]: print(dates[0]) 2013-01-01 00:00:00 In [10]: print(dates[0].strftime('%Y-%m-%d')) 2013-01-01 to_pydatetime returns a NumPy array of Python datetime.datetime o ... -
使用DatetimeIndex重塑Pandas数据框以生成网格(Reshape Pandas dataframe with DatetimeIndex to make grid)[2023-02-19]
df.index = [df.index.date, df.index.hour + df.index.minute / 60] df[0].unstack() 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 2013-01-01 1 2 3 4 5 6 7 8 9 10 2013-01-02 49 50 51 52 53 54 55 56 ... -
您可以执行以下操作: dfST['timestamp'] = pd.to_datetime(dfST['timestamp']) to_datetime()将推断日期列的格式。 如果列包含非日期值,您还可以传递errors='coerce' 。 完成上述操作后,您将能够创建仅包含日期值的新列: dfST['new_date_column'] = dfST['timestamp'].dt.date You can do the following: dfST['timestamp'] = pd.to_da ...
-
DatetimeIndex偏移量(DatetimeIndex offset with)[2022-04-11]
原因是在第一种情况下,您有一个频率为5分钟的常规DatetimeIndex。 因此,整数1将被解释为频率的一个单位(5分钟)。 在第二种情况下,由于切片,你不再有常规时间序列,并且DatetimeIndex不再有频率( z.index.freq将给出None,而grid_proto.index.freq将给出5分钟)。 要解决这个问题,你可以明确地添加5分钟: In [22]: import datetime as dt In [23]: z.index[0] + dt.timedelta(minutes ... -
import datetime as dt import pandas as pd df = pd.DataFrame({'year': [2015, 2016], 'month': [12, 1], 'day': [31, 1], 'hour': [23, 1]}) # returns datetime objects df['Timestamp'] = df.apply(lambd ...