def test_series_tz_convert(self): rng = date_range('1/1/2011', periods=200, freq='D', tz='US/Eastern') ts = Series(1, index=rng) result = ts.tz_convert('Europe/Berlin') assert result.index.tz.zone == 'Europe/Berlin' # can't convert tz-naive rng = date_range('1/1/2011', periods=200, freq='D') ts = Series(1, index=rng) with pytest.raises(TypeError, match="Cannot convert tz-naive"): ts.tz_convert('US/Eastern')
def test_arith_utc_convert(self): rng = date_range("1/1/2011", periods=100, freq="H", tz="utc") perm = np.random.permutation(100)[:90] ts1 = Series(np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")) perm = np.random.permutation(100)[:90] ts2 = Series(np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")) result = ts1 + ts2 uts1 = ts1.tz_convert("utc") uts2 = ts2.tz_convert("utc") expected = uts1 + uts2 self.assertEqual(result.index.tz, pytz.UTC) tm.assert_series_equal(result, expected)
def test_series_align_aware(self): idx1 = date_range('2001', periods=5, freq='H', tz='US/Eastern') ser = Series(np.random.randn(len(idx1)), index=idx1) ser_central = ser.tz_convert('US/Central') # # different timezones convert to UTC new1, new2 = ser.align(ser_central) assert new1.index.tz == pytz.UTC assert new2.index.tz == pytz.UTC
def test_series_add_tz_mismatch_converts_to_utc(self): rng = date_range('1/1/2011', periods=100, freq='H', tz='utc') perm = np.random.permutation(100)[:90] ser1 = Series(np.random.randn(90), index=rng.take(perm).tz_convert('US/Eastern')) perm = np.random.permutation(100)[:90] ser2 = Series(np.random.randn(90), index=rng.take(perm).tz_convert('Europe/Berlin')) result = ser1 + ser2 uts1 = ser1.tz_convert('utc') uts2 = ser2.tz_convert('utc') expected = uts1 + uts2 assert result.index.tz == pytz.UTC tm.assert_series_equal(result, expected)
def test_series_add_tz_mismatch_converts_to_utc(self): rng = date_range("1/1/2011", periods=100, freq="H", tz="utc") perm = np.random.permutation(100)[:90] ser1 = Series(np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")) perm = np.random.permutation(100)[:90] ser2 = Series(np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")) result = ser1 + ser2 uts1 = ser1.tz_convert("utc") uts2 = ser2.tz_convert("utc") expected = uts1 + uts2 assert result.index.tz == pytz.UTC tm.assert_series_equal(result, expected)
def test_arith_utc_convert(self): rng = date_range('1/1/2011', periods=100, freq='H', tz='utc') perm = np.random.permutation(100)[:90] ts1 = Series(np.random.randn(90), index=rng.take(perm).tz_convert('US/Eastern')) perm = np.random.permutation(100)[:90] ts2 = Series(np.random.randn(90), index=rng.take(perm).tz_convert('Europe/Berlin')) result = ts1 + ts2 uts1 = ts1.tz_convert('utc') uts2 = ts2.tz_convert('utc') expected = uts1 + uts2 self.assert_(result.index.tz == pytz.UTC) assert_series_equal(result, expected)
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self): rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern") ser = Series(np.random.randn(len(rng)), index=rng) ts_moscow = ser.tz_convert("Europe/Moscow") result = ser + ts_moscow assert result.index.tz is pytz.utc result = ts_moscow + ser assert result.index.tz is pytz.utc
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self): rng = date_range('1/1/2011', periods=10, freq='H', tz='US/Eastern') ser = Series(np.random.randn(len(rng)), index=rng) ts_moscow = ser.tz_convert('Europe/Moscow') result = ser + ts_moscow assert result.index.tz is pytz.utc result = ts_moscow + ser assert result.index.tz is pytz.utc
def test_series_tz_convert(self): rng = date_range('1/1/2011', periods=200, freq='D', tz='US/Eastern') ts = Series(1, index=rng) result = ts.tz_convert('Europe/Berlin') assert result.index.tz.zone == 'Europe/Berlin' # can't convert tz-naive rng = date_range('1/1/2011', periods=200, freq='D') ts = Series(1, index=rng) tm.assert_raises_regex(TypeError, "Cannot convert tz-naive", ts.tz_convert, 'US/Eastern')
def test_series_frame_tz_convert(self): rng = date_range('1/1/2011', periods=200, freq='D', tz='US/Eastern') ts = Series(1, index=rng) result = ts.tz_convert('Europe/Berlin') self.assert_(result.index.tz.zone == 'Europe/Berlin') df = DataFrame({'a': 1}, index=rng) result = df.tz_convert('Europe/Berlin') expected = DataFrame({'a': 1}, rng.tz_convert('Europe/Berlin')) self.assert_(result.index.tz.zone == 'Europe/Berlin') assert_frame_equal(result, expected) df = df.T result = df.tz_convert('Europe/Berlin', axis=1) self.assert_(result.columns.tz.zone == 'Europe/Berlin') assert_frame_equal(result, expected.T)
def test_tz_convert(self): rng = date_range('1/1/2011', periods=100, freq='H') ts = Series(1, index=rng) result = ts.tz_convert('utc') self.assert_(result.index.tz.zone == 'UTC') df = DataFrame({'a': 1}, index=rng) result = df.tz_convert('utc') expected = DataFrame({'a': 1}, rng.tz_convert('UTC')) self.assert_(result.index.tz.zone == 'UTC') assert_frame_equal(result, expected) df = df.T result = df.tz_convert('utc', axis=1) self.assert_(result.columns.tz.zone == 'UTC') assert_frame_equal(result, expected.T)
def test_equal_join_ensure_utc(self): rng = date_range('1/1/2011', periods=10, freq='H', tz='US/Eastern') ts = Series(np.random.randn(len(rng)), index=rng) ts_moscow = ts.tz_convert('Europe/Moscow') result = ts + ts_moscow self.assert_(result.index.tz is pytz.utc) result = ts_moscow + ts self.assert_(result.index.tz is pytz.utc) df = DataFrame({'a': ts}) df_moscow = df.tz_convert('Europe/Moscow') result = df + df_moscow self.assert_(result.index.tz is pytz.utc) result = df_moscow + df self.assert_(result.index.tz is pytz.utc)
def test_equal_join_ensure_utc(self): rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern") ts = Series(np.random.randn(len(rng)), index=rng) ts_moscow = ts.tz_convert("Europe/Moscow") result = ts + ts_moscow self.assertIs(result.index.tz, pytz.utc) result = ts_moscow + ts self.assertIs(result.index.tz, pytz.utc) df = DataFrame({"a": ts}) df_moscow = df.tz_convert("Europe/Moscow") result = df + df_moscow self.assertIs(result.index.tz, pytz.utc) result = df_moscow + df self.assertIs(result.index.tz, pytz.utc)
def test_with_local_timezone_pytz(self): # see gh-5430 local_timezone = pytz.timezone("America/Los_Angeles") start = datetime(year=2013, month=11, day=1, hour=0, minute=0, tzinfo=pytz.utc) # 1 day later end = datetime(year=2013, month=11, day=2, hour=0, minute=0, tzinfo=pytz.utc) index = pd.date_range(start, end, freq="H") series = Series(1, index=index) series = series.tz_convert(local_timezone) result = series.resample("D", kind="period").mean() # Create the expected series # Index is moved back a day with the timezone conversion from UTC to # Pacific expected_index = pd.period_range(start=start, end=end, freq="D") - offsets.Day() expected = Series(1, index=expected_index) tm.assert_series_equal(result, expected)
def test_series_frame_tz_convert(self): rng = date_range("1/1/2011", periods=200, freq="D", tz="US/Eastern") ts = Series(1, index=rng) result = ts.tz_convert("Europe/Berlin") self.assertEqual(result.index.tz.zone, "Europe/Berlin") df = DataFrame({"a": 1}, index=rng) result = df.tz_convert("Europe/Berlin") expected = DataFrame({"a": 1}, rng.tz_convert("Europe/Berlin")) self.assertEqual(result.index.tz.zone, "Europe/Berlin") assert_frame_equal(result, expected) df = df.T result = df.tz_convert("Europe/Berlin", axis=1) self.assertEqual(result.columns.tz.zone, "Europe/Berlin") assert_frame_equal(result, expected.T) # can't convert tz-naive rng = date_range("1/1/2011", periods=200, freq="D") ts = Series(1, index=rng) tm.assertRaisesRegexp(TypeError, "Cannot convert tz-naive", ts.tz_convert, "US/Eastern")
def test_with_local_timezone_dateutil(self): # see gh-5430 local_timezone = 'dateutil/America/Los_Angeles' start = datetime(year=2013, month=11, day=1, hour=0, minute=0, tzinfo=dateutil.tz.tzutc()) # 1 day later end = datetime(year=2013, month=11, day=2, hour=0, minute=0, tzinfo=dateutil.tz.tzutc()) index = pd.date_range(start, end, freq='H', name='idx') series = Series(1, index=index) series = series.tz_convert(local_timezone) result = series.resample('D', kind='period').mean() # Create the expected series # Index is moved back a day with the timezone conversion from UTC to # Pacific expected_index = (pd.period_range(start=start, end=end, freq='D', name='idx') - offsets.Day()) expected = Series(1, index=expected_index) assert_series_equal(result, expected)
def test_series_frame_tz_convert(self): rng = date_range('1/1/2011', periods=200, freq='D', tz='US/Eastern') ts = Series(1, index=rng) result = ts.tz_convert('Europe/Berlin') self.assertEqual(result.index.tz.zone, 'Europe/Berlin') df = DataFrame({'a': 1}, index=rng) result = df.tz_convert('Europe/Berlin') expected = DataFrame({'a': 1}, rng.tz_convert('Europe/Berlin')) self.assertEqual(result.index.tz.zone, 'Europe/Berlin') assert_frame_equal(result, expected) df = df.T result = df.tz_convert('Europe/Berlin', axis=1) self.assertEqual(result.columns.tz.zone, 'Europe/Berlin') assert_frame_equal(result, expected.T) # can't convert tz-naive rng = date_range('1/1/2011', periods=200, freq='D') ts = Series(1, index=rng) tm.assertRaisesRegexp(TypeError, "Cannot convert tz-naive", ts.tz_convert, 'US/Eastern')
def test_series_frame_tz_convert(self): rng = date_range('1/1/2011', periods=200, freq='D', tz='US/Eastern') ts = Series(1, index=rng) result = ts.tz_convert('Europe/Berlin') self.assertEqual(result.index.tz.zone, 'Europe/Berlin') df = DataFrame({'a': 1}, index=rng) result = df.tz_convert('Europe/Berlin') expected = DataFrame({'a': 1}, rng.tz_convert('Europe/Berlin')) self.assertEqual(result.index.tz.zone, 'Europe/Berlin') assert_frame_equal(result, expected) df = df.T result = df.tz_convert('Europe/Berlin', axis=1) self.assertEqual(result.columns.tz.zone, 'Europe/Berlin') assert_frame_equal(result, expected.T) # can't convert tz-naive rng = date_range('1/1/2011', periods=200, freq='D') ts = Series(1, index=rng) assertRaisesRegexp(TypeError, "Cannot convert tz-naive", ts.tz_convert, 'US/Eastern')
def test_tz_convert(self): rng = date_range('1/1/2011', periods=100, freq='H') ts = Series(1, index=rng) result = ts.tz_convert('utc') self.assert_(result.index.tz.zone == 'UTC')
def schedule_charging_station( sensor: Sensor, start: datetime, end: datetime, resolution: timedelta, soc_at_start: float, soc_targets: pd.Series, soc_min: Optional[float] = None, soc_max: Optional[float] = None, roundtrip_efficiency: Optional[float] = None, prefer_charging_sooner: bool = True, price_sensor: Optional[Sensor] = None, round_to_decimals: Optional[int] = 6, ) -> Union[pd.Series, None]: """Schedule a charging station asset based directly on the latest beliefs regarding market prices within the specified time window. For the resulting consumption schedule, consumption is defined as positive values. Todo: handle uni-directional charging by setting the "min" or "derivative min" constraint to 0 """ # Check for required Sensor attributes sensor.check_required_attributes([("capacity_in_mw", (float, int))]) # Check for round-trip efficiency if roundtrip_efficiency is None: # Get default from sensor, or use 100% otherwise roundtrip_efficiency = sensor.get_attribute("roundtrip_efficiency", 1) if roundtrip_efficiency <= 0 or roundtrip_efficiency > 1: raise ValueError( "roundtrip_efficiency expected within the interval (0, 1]") # Check for min and max SOC, or get default from sensor if soc_min is None: # Can't drain the EV battery by more than it contains soc_min = sensor.get_attribute("min_soc_in_mwh", 0) if soc_max is None: # Lacking information about the battery's nominal capacity, we use the highest target value as the maximum state of charge soc_max = sensor.get_attribute("max_soc_in_mwh", max(soc_targets.values)) # Check for known prices or price forecasts, trimming planning window accordingly prices, (start, end) = get_prices( (start, end), resolution, price_sensor=price_sensor, sensor=sensor, allow_trimmed_query_window=True, ) # soc targets are at the end of each time slot, while prices are indexed by the start of each time slot soc_targets = soc_targets.tz_convert("UTC") start = pd.Timestamp(start).tz_convert("UTC") end = pd.Timestamp(end).tz_convert("UTC") soc_targets = soc_targets[start + resolution:end] # Add tiny price slope to prefer charging now rather than later, and discharging later rather than now. # We penalise the future with at most 1 per thousand times the price spread. if prefer_charging_sooner: prices = add_tiny_price_slope(prices, "event_value") # Set up commitments to optimise for commitment_quantities = [initialize_series(0, start, end, resolution)] # Todo: convert to EUR/(deviation of commitment, which is in MW) commitment_upwards_deviation_price = [ prices.loc[start:end - resolution]["event_value"] ] commitment_downwards_deviation_price = [ prices.loc[start:end - resolution]["event_value"] ] # Set up device constraints (only one device for this EMS) columns = [ "equals", "max", "min", "derivative equals", "derivative max", "derivative min", ] device_constraints = [initialize_df(columns, start, end, resolution)] device_constraints[0]["equals"] = soc_targets.shift( -1, freq=resolution ).values * (timedelta(hours=1) / resolution) - soc_at_start * ( timedelta(hours=1) / resolution ) # shift "equals" constraint for target SOC by one resolution (the target defines a state at a certain time, # while the "equals" constraint defines what the total stock should be at the end of a time slot, # where the time slot is indexed by its starting time) device_constraints[0]["min"] = (soc_min - soc_at_start) * ( timedelta(hours=1) / resolution) device_constraints[0]["max"] = (soc_max - soc_at_start) * ( timedelta(hours=1) / resolution) if sensor.get_attribute("is_strictly_non_positive"): device_constraints[0]["derivative min"] = 0 else: device_constraints[0]["derivative min"] = ( sensor.get_attribute("capacity_in_mw") * -1) if sensor.get_attribute("is_strictly_non_negative"): device_constraints[0]["derivative max"] = 0 else: device_constraints[0]["derivative max"] = sensor.get_attribute( "capacity_in_mw") # Apply round-trip efficiency evenly to charging and discharging device_constraints[0][ "derivative down efficiency"] = roundtrip_efficiency**0.5 device_constraints[0][ "derivative up efficiency"] = roundtrip_efficiency**0.5 # Set up EMS constraints (no additional constraints) columns = ["derivative max", "derivative min"] ems_constraints = initialize_df(columns, start, end, resolution) ems_schedule, expected_costs, scheduler_results = device_scheduler( device_constraints, ems_constraints, commitment_quantities, commitment_downwards_deviation_price, commitment_upwards_deviation_price, ) if scheduler_results.solver.termination_condition == "infeasible": # Fallback policy if the problem was unsolvable charging_station_schedule = fallback_charging_policy( sensor, device_constraints[0], start, end, resolution) else: charging_station_schedule = ems_schedule[0] # Round schedule if round_to_decimals: charging_station_schedule = charging_station_schedule.round( round_to_decimals) return charging_station_schedule