datetimeindex

Pandas get_loc with datetimeindex failing

女生的网名这么多〃 提交于 2021-01-29 10:17:45
问题 I have a time-indexed dataframe, and I want to find what row number matches the closest time to a given time. For example,: import pandas as pd findtime = pd.Timestamp('2019-12-12 10:19:25', tz='US/Eastern') start = pd.Timestamp('2019-12-12 0:0:0', tz='US/Eastern') end = pd.Timestamp('2019-12-13 0:0:0', tz='US/Eastern') testindex = pd.date_range(start, end, freq='5s') testindex.get_loc(findtime, method='nearest') However, this throws an error: UFuncTypeError: ufunc 'subtract' cannot use

Pandas DatetimeIndex: Number of periods in a frequency string?

回眸只為那壹抹淺笑 提交于 2021-01-29 08:24:41
问题 How can I get a count of the number of periods in a Pandas DatetimeIndex using a frequency string (offset alias)? For example, let's say I have the following DatetimeIndex: idx = pd.date_range("2019-03-01", periods=10000, freq='5T') I would like to know how many 5 minute periods are in a week, or '7D'. I can calculate this "manually": periods = (7*24*60)//5 Or I can get the length of a dummy index: len(pd.timedelta_range(start='1 day', end='8 days', freq='5T')) Neither approach seems very

xarray: coords conversion to datetime64

自闭症网瘾萝莉.ら 提交于 2021-01-29 05:18:58
问题 I have a NetCDF4 file that i'm handling using xarray. The dataset has a "time" coordinate as dtype=object and i would like to convert it ot datetime64 in order to simplify plotting of the variables contained in the file. My plan was to create a new time coordinate called "time1" using ds.assign_coords(time1=pd.to_datetime(ds.time.values,infer_datetime_format=True)) and then delete the old one. But i get a new coordinate still as dtype=object. here's how the new dataset looks like What am i

Find values from a column in a DF at very specific times for every unique date

夙愿已清 提交于 2021-01-28 20:01:47
问题 I asked this question and I got an answer which works for a general case with sequential and non missing data but not for my case specifically. I have a DF that looks as follows. eventTime MeteredEnergy Demand RunningHoursLamps 6/7/2018 0:00 67.728 64 1037.82 6/7/2018 1:00 67.793 64 1038.82 6/7/2018 2:00 67.857 64 1039.82 6/7/2018 3:00 67.922 64 1040.82 6/7/2018 4:00 67.987 64 1041.82 6/7/2018 5:00 64 1042.82 6/7/2018 6:00 1043.43 6/7/2018 23:00 68.288 6/8/2018 0:00 67.728 64 1037.82 6/8/2018

Convert datetime64[ns] column to DatetimeIndex in pandas

杀马特。学长 韩版系。学妹 提交于 2021-01-28 14:24:05
问题 One of the packages that I am working with has a pre-requisite that the index of the data frame needs to be a pandas DatetimeIndex. So, I have been trying to convert a column of the data type datetime64[ns] to DatetimeIndex with no success. Here are my attempts: import pandas as pd my_data = [[1,'2019-05-01 04:00:00'], [2, '2019-05-01 04:01:00'], [3, '2019-05-01 04:02:00']] test = pd.DataFrame(my_data, columns=['count', 'datetime']) print(test.dtypes.value_counts()) # Attempt using pd

Creating pandas DatetimeIndex in Dataframe from DST aware datetime objects

微笑、不失礼 提交于 2021-01-28 06:28:28
问题 From an online API I gather a series of data points, each with a value and an ISO timestamp. Unfortunately I need to loop over them, so I store them in a temporary dict and then create a pandas dataframe from that and set the index to the timestamp column (simplified example): from datetime import datetime import pandas input_data = [ '2019-09-16T06:44:01+02:00', '2019-11-11T09:13:01+01:00', ] data = [] for timestamp in input_data: _date = datetime.fromisoformat(timestamp) data.append({'time'

How to filter a pandas DatetimeIndex by day of week and hour in the day

泪湿孤枕 提交于 2020-12-26 08:13:18
问题 I have a pandas DatetimeIndex and I would like to filter the index by the criterion that the day of the week and hour of the day matches a list. For example, I have of list of tuples indicating valid (day of week, hour, minute) for each TimeStamp: [(4, 6), (5, 7)] The final index should only contain date times that are Friday(day_of_week = 4) hour 6 or Saturday(day_of_week = 5) hour 7. Lets say the input data frame is like: 2016-04-02 06:30:00 1 2016-04-02 06:45:00 2 2016-04-02 07:00:00 3